0

I have a model called CarModel and a model called CarModelImage. I want to be able to have images be associated to multiple CarModels. For this I tried using a ManyToManyField.

When I visit the admin page for CarModelImage I get the following error.

ProgrammingError at /admin/cars/carmodelimage/

Exception Value: column cars_carmodelimage.model_id does not exist

Why is it that I can use the many to many in CarModel but not in CarModelImage?

Is there a way I can add id to all my models without having to drop the database?

class CarModel(models.Model):
    title = models.CharField(max_length=80)
    category = models.ManyToManyField(CarCategory)
    ...

    class Meta:
        verbose_name_plural = "Car Models"

    def __str__(self):
        return self.title


class CarModelImage(models.Model):
    timestamp = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to='cars/')
    model = models.ManyToManyField(CarModel)
    # This is what I had before: model = models.ForeignKey(default=1, on_delete=models.CASCADE, to='cars.CarModel')

    def filename(self):
        return basename(self.image.name)

    class Meta:
        verbose_name_plural = "Car Model Images"
        ordering = ["timestamp"]

    def __str__(self):
        return self.filename()
nandesuka
  • 699
  • 8
  • 22

0 Answers0