将用户模型导出到不同的应用程

时间:2018-01-24 15:46:31

标签: python django

我有一个项目有一个应用程序,用户,他们的帖子和其他模型位于。现在我想分离用户模型并将其放入不同的应用程序,称为用户 我该如何正确地做到这一点?我如何在旧应用程序中使用此模型?
第一个应用程序的用户模型如下所示:

class User(AbstractUser):

    class Meta:
        permissions = (
            ('can_do_this', 'Permission1'),
            ('can_do_that', 'Permission2'),
            ...,
        )

第二个(用户)应用的用户模型如下所示:

class ExternalUser(AbstractUser):

    class Meta:
        permissions = (
            ('can_do_this', 'Permission1'),
            ('can_do_that', 'Permission2'),
            ...,
        )

我已完成django-admin startapp users并将用户的模型代码复制粘贴到已创建的app models.py中。

然后我尝试makemigrations,但失败了,显示:

SystemCheckError: System check identified some issues:

ERRORS:
photogal.User.groups: (fields.E304) Reverse accessor for 'User.groups' 
clashes with reverse accessor for 'ExternalUser.groups'.
    HINT: Add or change a related_name argument to the definition for 
'User.groups' or 'ExternalUser.groups'.
photogal.User.user_permissions: (fields.E304) Reverse accessor for 
'User.user_permissions' clashes with reverse accessor for 
'ExternalUser.user_permissions'.
    HINT: Add or change a related_name argument to the definition for 
'User.user_permissions' or 'ExternalUser.user_permissions'.
 users.ExternalUser.groups: (fields.E304) Reverse accessor for 
'ExternalUser.groups' clashes with reverse accessor for 'User.groups'.
    HINT: Add or change a related_name argument to the definition for 
'ExternalUser.groups' or 'User.groups'.
users.ExternalUser.user_permissions: (fields.E304) Reverse accessor for 
'ExternalUser.user_permissions' clashes with reverse accessor for 
'User.user_permissions'.
    HINT: Add or change a related_name argument to the definition for 
'ExternalUser.user_permissions' or 'User.user_permissions'.

添加related_name =' +'对旧用户模型产生了:

ERRORS:
<class 'django.contrib.auth.admin.UserAdmin'>: (admin.E020) The value of 
'filter_horizontal[0]' must be a many-to-many field.
<class 'django.contrib.auth.admin.UserAdmin'>: (admin.E020) The value of 
'filter_horizontal[1]' must be a many-to-many field.

谢谢!

1 个答案:

答案 0 :(得分:1)

只需从上一个应用程序中删除旧的用户模型,然后将新的用户模型放入新的应用程序中,这样最终您只有一个扩展AbstractUser类的User类。

至于您的second issue,请尝试this

我希望这会有所帮助。