I have 2 models, Post and Category:
class Category(models.Model):
# some fields
class Post(models.Model):
category = models.ForeignKey(Category, related_name='posts',
on_delete=models.SET(get_default_category()))
get_default_category is basically just the shortcut for get_or_create.
When I try to delete some object (using .delete() or from the admin app), I get the following exception:
IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key constraint fails (`tatatata`.`discussions_post`, CONSTRAINT `category_id_refs_id_783afa2fc9c73207` FOREIGN KEY (`category_id`) REFERENCES `discussions_category` (`id`))')
What’s wrong?
Try passing a callable instead of
get_default_category().