2

I have a model of product and another model called Course that inherits the product but has a video field and an author Which is a ForeignKey with A teacher model which inherits from the user model which inherits from AbstractUser

Related Models:

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)

when I'm trying to make migrations it says this:

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.

I'm using PostgreSQL for my db, It didn't let me flush it so I Dropped it and ReCreated a new one But I Still can't makemigrations

I tried these answers: 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"

I faced these kinds of errors before but one thing I couldn't figure out is that it isn't related to a field so I can't fix it

Python: 3.10.6
Django: 4.1
PostgreSQL: psql 14.6
OS: Ubuntu 22.04 (Not a VM)

BGOPC
  • 201
  • 9

1 Answers1

0

I figured out that so many repetitive inheritances made this error not sure why but this solves it:

class BaseProduct(models.Model):
    class Meta:
        abstract = True

    name = models.CharField(max_length=100, null=False, blank=True)
    shoppers = models.ManyToManyField(User)
    tumb = models.ImageField(upload_to=course_tumb_directory_path, null=False, blank=True)
    lastUpdate = models.DateTimeField(auto_now=True)
    price = models.DecimalField(null=False, default=1000000, max_digits=7, decimal_places=0)


class Product(BaseProduct):
    count = models.DecimalField(null=False, default=1, max_digits=7, decimal_places=0)

class Course(BaseProduct):
    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, related_name='courses')

Inheriting from an abstract model instead of repetitive Inheritance

BGOPC
  • 201
  • 9