【发布时间】:2019-11-09 20:28:02
【问题描述】:
编辑:+ 患者
当我检查我的代码时,我注意到某些模型具有属性“密码”作为 PostgreSQL 中的数据库列,而这些模型没有该属性,属性“密码”和表之间的唯一关系是表的模型有一个指向作为 AbstractBaseUser 扩展的模型的 FK。
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 的列名“密码”,我不知道它们为什么存在,因为我没有在模型中声明该属性。
【问题讨论】:
-
您的问题是什么? Doctor 显然继承自 AbstractBaseUser。 (您还没有显示 Patient。)
-
@DanielRoseman 问题是为什么当我迁移到 postgres 时它显示“密码”列
-
因为它继承自 AbstractBaseUser,正如我所说。
标签: python django django-models django-rest-framework