I have three related models - Document, Collection, and CollectionDocument. A Collection object is made up of a group of documents.
class Document(Model):
document_id = models.AutoField(primary_key=True)
class Collection(Model):
collection_id = models.AutoField(primary_key=True)
document = models.ManyToManyField(Document, through='CollectionDocument', related_name='collections',)
class CollectionDocument(Model):
collection_id = models.ForeignKey(Collection, on_delete=models.CASCADE, )
document = models.ForeignKey(Document, on_delete=models.CASCADE, )In the Djangop Admin, I have a DocumentAdmin and a CollectionAdmin with the CollectionDocument as inlines in the CollectionAdmin.
When I delete all the documents associated with a Collection, I also want the Collection to be deleted. Right now, when I delete the documents in a Collection, the CollectioDocument is empty, but the Collection still exists without any documents associated with it.
I have tried several ways to do it using pre- and post-delete signals for the CollectionDocument model, but nothing seems to work. Have I structured my models incorrectly? How do I get the Collection to be deleted when all the associated Documents are deleted?
Thanks!
Mark