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

时间:2018-07-10 10:35:16

标签: python django django-models celery django-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 个答案:

答案 0 :(得分:0)

->在哪里定义?这是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中吗?