首页 文章

在Django中扩展AbstractUser的正确方法?

提问于
浏览
0

我'm trying to integrate two django apps where each had their individual auths working. To do that, I' m试图继承AbstractUser而不是User . 我正在关注PyBB docsDjango#substituting_custom_model . 我删除了所有应用程序中的所有迁移文件,除了它们各自的 init .py(包括从我的站点包中的PyBB库中迁移) . 我've also changed the Mysql database to a blank one to start afresh and I' m试图将AbstractUser子类化,如下所示 .

我的Models.py:

from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser

from django.db import models
class Student_User(models.Model):
    """
    Table to store accounts
    """
    su_student = models.OneToOneField(AbstractUser)

    USERNAME_FIELD = 'su_student'

    su_type = models.PositiveSmallIntegerField(db_column='su_type', default=0)
    su_access = models.TextField(db_column='su_access', default='')
    su_packs = models.TextField(db_column='su_packs', default='')

    REQUIRED_FIELDS = []

    def __unicode__(self):
        return str(self.su_student)

我的settings.py:

AUTH_USER_MODEL = "app.Student_User"
PYBB_PROFILE_RELATED_NAME = 'pybb_profile'

在为我的主应用程序运行makemigrations时,出现此错误:

app.Student_User.su_student: (fields.E300) Field defines a relation with model 'AbstractUser', which is either not installed, or is abstract.

我如何实现我想在这里做的事情?

PS:该应用程序与没有username_field或required_field的用户的onetoone工作正常 .

PPS:我刚刚在contrib.auth.models中检查了AbstractUser模型,它有 class Meta: abstract = True . 好的,它的抽象,我仍然如何解决这个问题?我只需要一次登录,目前,我的网站的两个部分,虽然通过网址连接,要求单独登录,不要检测对方登录 . 我需要做什么呢?

1 回答

  • 2

    你不能与抽象模型 Build 一对一的关系;根据定义,抽象模型从未实际实例化 .

    AbstractUser应该是继承的 . 你的结构应该是:

    class Student_User(AbstractUser):
        ...
    

相关问题