我的模型如下。
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(AbstractUser):
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
('O', 'Other'),
)
USER_TYPE = (
('S', 'Super Admin'),
('A', 'Admin'),
('P', 'Patient'),
('D', 'Doctor'),
('U', 'User'),
)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
date_of_birth = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
user_type = models.CharField(max_length=1, choices=USER_TYPE)
email = models.EmailField(unique=True)
phone = models.CharField(max_length=15, blank=True, null=True)
address = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return f"{self.first_name} {self.last_name}"
我收到如下消息。
It is impossible to add a non-nullable field 'password' to user without specifying a default. This is because the database needs something to populate existing rows.
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit and manually define a default value in models.py.
数据库中必须有一些用户对象,其密码字段为空值。这会阻止迁移应用,因为该字段不可为空。解决此问题的方法是检查数据库条目并设置密码,或者删除没有密码的用户。两种方法都应该可以解决您的问题。