I have a model:
class picture(models.Model):
name = models.CharField(null=False, blank=False, max_length=128)
collections = models.ManyToManyField('collection', null=False, blank=False)
sizes = models.ManyToManyField('picture_size', null=False, blank=False)
papers = models.ManyToManyField('picture_paper', null=False, blank=False)
base_price = models.DecimalField(decimal_places=2, null=False, blank=False, default=2000, max_digits=10)
img_icon = models.FileField(blank=False, null=False, upload_to='pic_icons')
img_large = models.FileField(blank=False, null=False, upload_to='pic_large')
img_huge = models.FileField(blank=False, null=False, upload_to='pic_huge')
is_active = models.BooleanField(blank=False, null=False, default=True)
and picture_paper:
class picture_paper(models.Model):
name = models.CharField(null=False, blank=False, max_length=128)
price_per_ssm = models.DecimalField(null=False, blank=False, decimal_places=2, max_digits=10)
is_active = models.BooleanField(null=False, blank=False, default=True)
I register them models in admin and can create a picture_paper but can’t delete it:
(1054, “Unknown column ‘picture_paper_id’ in ‘where clause'”)
The SQL it is building is this:
'DELETE FROM `core_picture_papers` WHERE `picture_paper_id` IN (%s)'
explain core_picture_papers:
id int(11) NO PRI auto_increment
picture_id int(11) NO MUL
paper_id int(11) NO MUL
Is my model setup totally wrong (I’m trying to build a dictionary of paper kinds and hook them to pictures – a picture can relate to a few sizes or one etc)? Or there is something I’m missing?
I would say that you’re trying to remove a
picture_paperobject that is related to an existingpictureobject. As your M2M can’t be Null or Blank, you get an error when you’re trying to remove it.Take a look to the Django documentation about on_delete parameter. You will probably need to set it as
PROTECTEDand handle the exceptions properly.