编辑:+患者
在检查代码时,我注意到某些模型在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_ Patient和public.noteapp_doctor的表命名为“密码”,我不知道它们为什么会存在,因为我没有在模型中声明该属性。
答案 0 :(得分:0)
正如丹尼尔在评论中所写,这是预期的行为。您的类Doctor
和Patient
继承自AbstractBaseUser
,因此它们还将具有fields defined in AbstractBaseUser,分别是password
,last_login
和{{1 }}
查看django documentation on abstract base model classes,以获取有关此信息的更多信息。 如果您不熟悉继承的概念,则可以在Internet上找到一些解释继承的教程(例如this one)。
此外,您似乎想对is_active
,User
和Doctor
使用多态。您可能想看看django-polymorphic可以对此有所帮助。