代码之家  ›  专栏  ›  技术社区  ›  ofnowhere

Django-芹菜值错误:无法解析相关模型u'user.user'

  •  0
  • ofnowhere  · 技术社区  · 6 年前

    当我执行芹菜任务时,它会给我:

    值错误:无法解析相关模型u'user.user'

    stacktrace是

        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 回复  |  直到 6 年前
        1
  •  0
  •   tayfun    6 年前

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

    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 是在 INSTALLED_APPS ?