Generic relations to derived models won't allow for deletion of objects those models are attached to.
|Reported by:||Beetle_B||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
OK. Here's my scenario. I'm working with a blog, and with a comments app.
Let's say the comment's model is Comment. As it can be attached to any object, it has a generic foreign key to ContentType:
# Generic Foreign Key Fields content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField(_('object ID')) content_object = generic.GenericForeignKey()
Now I didn't like the Comment model: It was lacking an important field (comment_html). I didn't want to touch the comment's app's source code. So I created a new model that inherited from Comment and called it CommentMod. It consists of simply the extra field and overrides the save method.
The blog app has an Entry model. In that model, I put:
comments = GenericRelation(CommentMod)
The goal was that if I delete a blog entry, it should delete all comments associated with it.
However, when I try to delete a blog entry that has comments via the admin, I get an error. Upon observing the SQL, it turns out that it can't find an 'object_id' and 'content_type_id' columns in the CommentMod tables.
It can't do that because it's a derived model. Those columns are in the Comment tables.
I tried changing it to:
comments = GenericRelation(Comment)
But then if I try to delete Entry, I get a foreign key constraint problem from the DB (MySQL).
So it seems that I can't expect generic relations to work with derived models. I don't know if this is by design - if it's not, then it's a bug.
For now, I'll just create a GenericForeignKey in the derived models as well. Not sure if it will work...
Change History (13)
comment:1 Changed 7 years ago by Beetle_B
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:6 Changed 6 years ago by Beetle_B
- Cc mueen@… added
- Resolution invalid deleted
- Status changed from closed to reopened
- Version changed from 1.0 to SVN
comment:8 Changed 4 years ago by gabrielhurley
- Component changed from Contrib apps to contrib.contenttypes