Django 模型从另一个模型继承'password'字段,它的 PK 并显示在 PostgreSQL 中



编辑:+患者

当我检查我的代码时,我注意到一些模型在PostgreSQL中具有作为DB列的属性"password",而这些模型没有该属性,属性"password"和表之间的唯一关系是表的模型有一个指向模型的FK,该模型是AbstractBaseUser扩展。

django = ">=2.1.0"
djangorestframework = ">=3.9.2"
flake8 = ">=3.6.0,<3.7.0"
autopep8 = "*"
psycopg2 = "<2.8.0,>=2.7.5"
django-organizations = "==1.1.2"
django-countries = "*"
[requires]
python_version = "3.7"

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, 
PermissionsMixin
import datetime
from django.core.validators import MaxValueValidator
from django_countries.fields import CountryField
# Create your models here.

class UserManager(BaseUserManager):
def create_user(self, email, password = None, ** kwargs):
""
"Creates and saves a new User"
""
if not email:
raise ValueError('Users must have an email address')
user = self.model(email = self.normalize_email(email), ** kwargs)
user.set_password(password)
user.is_staff = False
user.is_active = True
user.is_doctor = False
user.save(using = self._db)
return user
def create_superuser(self, email, password, ** kwargs):
""
"Creates and saves a new super user"
""
user = self.create_user(email, password, ** kwargs)
user.is_staff = False
user.is_active = True
user.is_doctor = False
user.is_superuser = True
user.save(using = self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
""
"Custom user model that supports using email instead of username"
""
email = models.EmailField(max_length = 254, unique = True)
firstName = models.CharField(max_length = 30)
middleName = models.CharField(max_length = 30, blank = True)
firstSurname = models.CharField(max_length = 50)
lastSurname = models.CharField(max_length = 50, blank = True)
passwordHint = models.CharField(max_length = 20, blank = True, null = True)
creationDate = models.DateField(
default = datetime.date.today, blank = False, null = False)
lastLoginDate = models.DateField(
blank = True, null = True)
lastPasswordResetDate = models.DateField(blank = True, null = True)
is_active = models.BooleanField(
default = True)
is_staff = models.BooleanField(
default = False)
is_doctor = models.BooleanField(
default = False)
externalUserCode = models.CharField(max_length = 20, blank = True, null = True)
objects = UserManager()
USERNAME_FIELD = 'email'
class Doctor(AbstractBaseUser):
"""Custom user that has the information of the doctors"""
doctorID = models.OneToOneField(
User, on_delete=models.CASCADE, primary_key=True)
specialtyID = models.ManyToManyField(DoctorSpecialties)
identificationTypeID = models.ForeignKey('generic.IdentificationType',
on_delete=models.CASCADE)
identificationTypeNumber = models.PositiveIntegerField(
validators=[MaxValueValidator(9999999999)])
class Patient(AbstractBaseUser):
"""Custom user that has the information of the patient"""
id = models.AutoField(primary_key=True)
patient = models.ForeignKey(User, on_delete=models.CASCADE)
identificationType = models.ForeignKey('genericWS.IdentificationType',
on_delete=models.CASCADE)
identificationNumber = models.CharField(max_length=30, null=False)
patientAddress = models.CharField(max_length=30, null=False)

问题是,我有一个表public.noteapp_paient和public.noteapps_doctor的列名"password",我不知道它们存在的确切原因,因为我没有在模型中声明该属性。

正如Daniel在评论中所写,这是意料之中的行为。类DoctorPatient继承自AbstractBaseUser,因此它们也将具有在AbstractBaseUser中定义的字段,即passwordlast_loginis_active

查看关于抽象基本模型类的django文档,了解有关此方面的更多信息。如果继承的概念对你来说是新的,你可以在互联网上找到一些教程(例如本教程(来解释这一点。

此外,看起来您希望对UserDoctorPatient使用多态性。你可能想看看django-polymorphil,它在这方面有很大帮助。

最新更新