我正在使用 django-rq 使用 Redis 设置后台任务。Redis 运行良好,代码正在向 Redis 发送请求 - 但它正在引发异常。
Exception:
Traceback (most recent call last):
File "/Users/jeremyenglert/Documents/Sites/showzone/.venv/lib/python3.8/site-packages/rq/worker.py", line 1003, in perform_job
self.prepare_job_execution(job, heartbeat_ttl)
File "/Users/jeremyenglert/Documents/Sites/showzone/.venv/lib/python3.8/site-packages/rq/worker.py", line 893, in prepare_job_execution
self.procline(msg.format(job.func_name, job.origin, time.time()))
File "/Users/jeremyenglert/Documents/Sites/showzone/.venv/lib/python3.8/site-packages/rq/job.py", line 254, in func_name
self._deserialize_data()
File "/Users/jeremyenglert/Documents/Sites/showzone/.venv/lib/python3.8/site-packages/rq/job.py", line 222, in _deserialize_data
self._func_name, self._instance, self._args, self._kwargs = self.serializer.loads(self.data)
File "./api/models.py", line 5, in <module>
class PlayerProfile(models.Model):
File "/Users/jeremyenglert/Documents/Sites/showzone/.venv/lib/python3.8/site-packages/django/db/models/base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/jeremyenglert/Documents/Sites/showzone/.venv/lib/python3.8/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "/Users/jeremyenglert/Documents/Sites/showzone/.venv/lib/python3.8/site-packages/django/apps/registry.py", line 135, in check_apps_ready
settings.INSTALLED_APPS
File "/Users/jeremyenglert/Documents/Sites/showzone/.venv/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
self._setup(name)
File "/Users/jeremyenglert/Documents/Sites/showzone/.venv/lib/python3.8/site-packages/django/conf/__init__.py", line 64, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings
这是代码的简化版本。
# views.py
class PlayerProfileView(viewsets.ModelViewSet):
...
def get_queryset(self):
...
@action(methods=['get'], detail=False)
def update_all_profiles(self, request, pk=None):
update_player_profiles.delay(PlayerProfile)
return Response(status=status.HTTP_200_OK)
# update_player_profiles.py
@job
def update_player_profiles(model):
a_long_running_request_for_a_csv_file()
a_long_running_task_to_process_data()
a_long_running_task_to_save_the_data_to_a_django_model()
我怎样才能让它 django-rq 可以与 django 模型交互?还是有不同的问题在起作用?