Correct way to extend AbstractUser in Django?

耗尽温柔 提交于 2019-12-12 01:38:04

问题


I'm trying to integrate two django apps where each had their individual auths working. To do that, I'm trying to subclass AbstractUser instead of User. I'm following the PyBB docs and Django#substituting_custom_model. I've removed all migration files in all my apps apart from their individual init.py (including the migrations from the PyBB library sitting in my site-packages). I've also changed the Mysql database to a blank one to start afresh and I'm trying to subclass AbstractUser as shown below.

My 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)

My settings.py:

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

When running makemigrations for my primary app, I get this error:

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

How do I achieve what I am trying to do here?

PS: The app was working fine with onetoone with User without username_field or required_field.

PPS: I just checked the AbstractUser model in my contrib.auth.models and it has class Meta: abstract = True. Ok so its abstract, still, how do I resolve this? I just need one login, currently, two parts of my site, although connected through urls, ask for seperate logins and dont detect each others logins. What do I need to do for this?


回答1:


You can't have a one-to-one relationship with an abstract model; by definition, an abstract model is never actually instantiated.

AbstractUser is supposed to be inherited. Your structure should be:

class Student_User(AbstractUser):
    ...


来源:https://stackoverflow.com/questions/39566144/correct-way-to-extend-abstractuser-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!