繁体   English   中英

Django,扩展用户模型,ImportError:无法导入名称用户

[英]Django, extending the User model, ImportError: cannot import name User

我读到这与循环依赖关系有关,但我确实找不到解决方法。 我正在尝试获取(属于组织的)Users的数量,以及与用户有关的更多详细信息,但是似乎我无法真正在Organization模型中使用User模型。

我也尝试像return self.user_set.count()一样反转get_users_count但这也不起作用。

追溯

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/Manos/Projects/devboard/project/accounts/models.py", line 3, in <module>
    from project.organizations.models import Organization
  File "/Users/Manos/Projects/devboard/project/organizations/models.py", line 5, in <module>
    from project.accounts.models import User
ImportError: cannot import name User

组织模式

from uuidfield import UUIDField
from django.db import models
from project.accounts.models import User

class Organization(models.Model):

    id = UUIDField(primary_key=True, auto=True, db_index=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    name = models.CharField(max_length=200, unique=True)
    website = models.URLField(max_length=200, blank=True)

    def __str__(self):
        return self.name

    def get_users_count(self):
        return User.objects.filter(organization=self).count()

用户模型

class User(AbstractBaseUser):

    id = models.AutoField(primary_key=True)  # custom User models must have an integer PK
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    email = models.EmailField(max_length=255, unique=True, db_index=True)
    organization = models.ForeignKey(Organization, related_name="users", null=True)

    is_admin = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

将导入移动到get_users_count()方法中:

def get_users_count(self):
    from project.accounts.models import User
    return User.objects.filter(organization=self).count()

或完全删除导入并使用向后关系:

def get_users_count(self):
    return self.users.count()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM