在Django休息框架中,在使用自定义模型时,我需要给AUTH_USER_MODEL什么值?

In Django rest framework , on using custom models, what value I need to give to AUTH_USER_MODEL?

在django rest框架中,我在model文件夹中有不同的模型-

设置.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap3',
'rest_framework',
'myapp',
]


AUTH_USER_MODEL = 'myapp.models.UserModel.User'
  • 迈阿普
    • 设置
      • 德芙
    • 模型
      • 用户模型.py
      • 订单模型.py
      • 报告模型.py

用户模型.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class User(AbstractBaseUser, GuardianUserMixin ,PermissionsMixin):

id = models.AutoField(_('id'),unique=True,primary_key=True)
email = models.EmailField(_('email address'),unique=True)
last_name = models.CharField(_('last name'), max_length=100, blank=True)
first_name = models.CharField(_('first name'), max_length=100, blank=True)
parent_id = models.IntegerField(_('parent id'), default=0)
organization_id = models.IntegerField(_('organization id'), default=0, null=True)
created_at = models.DateTimeField(_('date joined '), default=timezone.now())
updated_at = models.DateTimeField(_('date modified '), default=timezone.now())
incorrect_login = models.IntegerField(_('incorrect login frequency'), default=0)
soft_delete = models.BooleanField(_('soft delete'), default=False, )
group = models.ForeignKey('auth.Group', null=True)

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['last_name, first_name, email,organization_id,group_id ']

class Meta:
    app_label = 'zeocuser'
    db_table = 'zeocuser_zeocuser'
    permissions = (
        (
            ('add_user_admin', 'add user admin'),
        )
    )


objects = UserManager()

现在,在settings.py for auth_user_model中,如果我给出以下值-auth_user_model='myapp.models.usermodel.user'它不接受并给出以下错误-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
File"<frozen importlib._bootstrap>", line 986, in _gcd_import
  File"<frozen importlib._bootstrap>", line 969, in _find_and_load
  File"<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File"<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File"<frozen importlib._bootstrap_external>", line 662, in exec_module
  File"<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File"/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/contrib/admin/models.py", line 32, in <module>
    class LogEntry(models.Model):
  File"/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/base.py", line 158, in __new__
    new_class.add_to_class(obj_name, obj)
  File"/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/base.py", line 299, in add_to_class
    value.contribute_to_class(cls, name)
  File"/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/fields/related.py", line 706, in contribute_to_class
    super(ForeignObject, self).contribute_to_class(cls, name, virtual_only=virtual_only)
  File"/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/fields/related.py", line 306, in contribute_to_class
    lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
  File"/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/fields/related.py", line 86, in lazy_related_operation
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
  File"/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/fields/related.py", line 84, in <genexpr>
    model_keys = (make_model_tuple(m) for m in models)
  File"/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/utils.py", line 13, in make_model_tuple
    app_label, model_name = model.split(".")
ValueError: too many values to unpack (expected 2)

但是如果我把我的用户模型直接放在一个models.py文件中(不放在models文件夹中),并给出

1
AUTH_USER_MODEL = 'myapp.User'

它工作得很好。但在这种情况下,我应该如何组织我的其他模型课程。请建议。


Django希望模型位于.models命名空间中。

正如https://docs.djangoproject.com/en/1.9/ref/applications/how applications are loaded第2点所述:

You must define or import all models in your application’s models.py or models/init.py. Otherwise, the application registry may not be fully populated at this point, which could cause the ORM to malfunction.

在您的情况下,您需要在myapp/models/uuu init_uuuy文件中导入模型:

1
2
3
from .UserModels import User, <other models>
from .OrderModels import <other models>
from .ReportingModels import <other models>

等等。