Python Django模型正在继承';密码';使用它的另一个模型的字段';s PK及其在PostgreSQL中的显示
编辑:+患者 当我检查代码时,我注意到一些模型在PostgreSQL中将属性“password”作为DB列,而这些模型没有该属性,属性“password”和表之间的唯一关系是表的模型有一个FK,指向一个作为AbstractBaseUser扩展的模型Python Django模型正在继承';密码';使用它的另一个模型的字段';s PK及其在PostgreSQL中的显示,python,django,django-models,django-rest-framework,Python,Django,Django Models,Django Rest Framework,编辑:+患者 当我检查代码时,我注意到一些模型在PostgreSQL中将属性“password”作为DB列,而这些模型没有该属性,属性“password”和表之间的唯一关系是表的模型有一个FK,指向一个作为AbstractBaseUser扩展的模型 django = ">=2.1.0" djangorestframework = ">=3.9.2" flake8 = ">=3.6.0,<3.7.0" autopep8 = "*" psycopg2 = "<2.8.0
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)
django=“>=2.1.0”
djangorestframework=“>=3.9.2”
Flake 8=“>=3.6.0,正如Daniel在评论中所写,这是预期的行为。您的类Doctor
和Patient
继承自AbstractBaseUser
,因此它们也将具有密码
,上次登录
和处于活动状态
有关这方面的更多信息,请查看。
如果继承的概念对你来说是新概念,你可以在互联网上找到一些教程(例如)来解释这一点
另外,看起来你想在那里为用户
、医生
和患者
使用多态性。你可能想看看哪些可以在这方面起到很大的帮助。你的问题是什么?医生显然是从AbstractBaseUser继承来的。(你还没有给患者看。)@DanielRoseman问题是为什么它会显示一个“正如我所说,当我迁移到PostgresB时,因为它继承自AbstractBaseUser。这回答了我的问题,以及一个与能够从abse用户类创建子用户相关的持续问题,非常感谢。