Django - Celery ValueError:无法解析相关模型 u'user.User'

Posted

技术标签:

【中文标题】Django - Celery ValueError:无法解析相关模型 u\'user.User\'【英文标题】:Django - Celery ValueError: Related model u'user.User' cannot be resolvedDjango - Celery ValueError:无法解析相关模型 u'user.User' 【发布时间】:2018-12-18 04:05:12 【问题描述】:

当我执行 celery 任务时,它给了我:

ValueError: 无法解析相关模型 u'user.User'

堆栈跟踪是

    Traceback (most recent call last):
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/celery/app/trace.py", line 375, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/celery/app/trace.py", line 632, in __protected_call__
    return self.run(*args, **kwargs)
  File "/Users/prince/work/magneto/set/facebook_pages/tasks.py", line 23, in analyze_page
    connected_facebook_page = get_connected_facebook_page(connected_facebook_page_id)
  File "/Users/prince/work/magneto/set/facebook_pages/utils.py", line 49, in get_connected_facebook_page
    return ConnectedUserPage.objects.get(id=connected_facebook_page_id)
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 374, in get
    num = len(clone)
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 232, in __len__
    self._fetch_all()
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 876, in execute_sql
    sql, params = self.as_sql()
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 428, in as_sql
    extra_select, order_by, group_by = self.pre_sql_setup()
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 46, in pre_sql_setup
    self.setup_query()
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 37, in setup_query
    self.select, self.klass_info, self.annotation_col_map = self.get_select()
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 194, in get_select
    for c in self.get_default_columns():
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 569, in get_default_columns
    column = field.get_col(alias)
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1008, in get_col
    return super(ForeignKey, self).get_col(alias, output_field or self.target_field)
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 909, in target_field
    return self.foreign_related_fields[0]
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 653, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 640, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 625, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model u'user.User' cannot be resolved

这里 ConnectedUserPage 是一个带有模式的模型:

class ConnectedUserPage(TimeStampedModel):
    user = models.ForeignKey('user.User', on_delete=models.PROTECT)
    page = models.ForeignKey(FacebookPage, on_delete=models.PROTECT)
    page_details = jsonb.JSONField()

我的版本是:

celery==4.1.1
django-celery-beat==1.1.1
django-celery-results==1.0.1
Django==1.11.13

如果我直接从用户应用导入用户模型,我会陷入循环导入。

任何帮助将不胜感激,现在卡在这个循环中很长一段时间了。

【问题讨论】:

【参考方案1】:

User 是在哪里定义的?这是 Django 自己的用户模型吗?如果是这种情况,您可以直接使用 User 模型:

from django.contrib.auth import get_user_model

User = get_user_model()


class ConnectedUserPage(TimeStampedModel):
    user = models.ForeignKey(User, unique=True)

或者你也可以这样做

from django.conf import settings

class ConnectedUserPage(TimeStampedModel):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

如果是您自己提供的 User 模型,您确定 userINSTALLED_APPS 中吗?

【讨论】:

以上是关于Django - Celery ValueError:无法解析相关模型 u'user.User'的主要内容,如果未能解决你的问题,请参考以下文章

django+django-celery+celery的整合实战

django + celery - 如何在我的 django 应用程序中为 celery 设置 crontab 计划?

django celery使用

django+celery实现异步任务

不同服务器上的 Django 和 celery,一旦任务完成,celery 能够向 django 发送回调

django+celery配置(定时任务)