我有一个产品模型和另一个名为Course的模型,该模型继承了产品,但有一个视频字段和一个作者,作者是一个外键,教师模型继承自用户模型,用户模型继承自AbstractUser
相关型号:
class User(AbstractUser):
username = models.SlugField(default="", null=False, db_index=True, blank=True) # forced by django admin problems :(
password = models.CharField(max_length=255, null=True)
email = models.EmailField(max_length=255, unique=True)
group = models.ManyToManyField(Group)
is_teacher = models.BooleanField(default=False, null=False)
is_seller = models.BooleanField(default=False, null=False)
phoneNum = PhoneNumberField(null=False, unique=True, default='')
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["username", "first_name", "last_name", "password"]
def save(self, *args, **kwargs):
self.username = slugify(self.first_name + self.last_name)
super().save(*args, **kwargs)
class Product(models.Model):
name = models.CharField(max_length=100, null=False, blank=True)
shoppers = models.ManyToManyField(User, related_name='shopper')
tumb = models.ImageField(upload_to=course_tumb_directory_path, null=False)
lastUpdate = models.DateTimeField(auto_now=True)
price = models.DecimalField(null=False, default=1000000, max_digits=7, decimal_places=0)
class Teacher(User):
TOPICS = [
("BP", "Basic Programming"),
("AP", "Advanced Programming"),
("CS", "Computer Science"),
("MS", "Mathematics"),
("CH", "Chemistry"),
("BL", "BioLogy"),
("PH", "physics"),
("EL", "Electronics"),
("RG", "Religious"),
("Or", "Other"),
]
topic = models.CharField(max_length=2, choices=TOPICS, default=TOPICS[-1][0])
class Course(Product):
video = models.FileField(upload_to=course_directory_path, null=True,
validators=[
FileExtensionValidator(allowed_extensions=['MOV', 'avi', 'mp4', 'webm', 'mkv'])])
author = models.ForeignKey(Teacher, on_delete=models.CASCADE, null=True)
当我试图进行迁移时,它是这样说的:
It is impossible to add a non-nullable field 'product_ptr' to course without specifying a default. This is because the database needs something to populate existing rows.
我正在使用PostgreSQL作为我的数据库,它不允许我刷新它,所以我放弃了它,并重新创建了一个新的,但我仍然不能使迁移
我尝试了以下答案:You are trying to add a non-nullable field 'new_field' to userprofile without a default
Django: You are trying to add a non-nullable field 'slug' to post without a default; we can't do that
It is impossible to add a non-nullable field 'id' to video without specifying a default
You are trying to add a non-nullable field 'id' to contact_info without a default
Error : "You are trying to add a non-nullable field"
我以前遇到过这种错误,但有一件事我搞不清楚,那就是它与字段无关,所以我无法修复它
Python语言:3.10.6
Django :4.1
PostgreSQL语言:psql 14.6
操作系统:Ubuntu 22.04(非虚拟机)
1条答案
按热度按时间hrysbysz1#
我发现,如此多的重复继承导致了这个错误,不知道为什么,但这解决了它:
从抽象模型继承,而不是重复继承