I’m trying to extend a library model through multi-table inheritance and this is what I ended up with:
class CompetitionMedia(InstagramMedia):
visible = models.BooleanField(default=True)
most_creative = models.BooleanField(default=False)
@receiver(post_save, sender=InstagramMedia, dispatch_uid='create_competition_media')
def create_competition_media(sender, instance, created, **kwargs):
competition_media = CompetitionMedia()
competition_media.instagrammedia = instance
competition_media.save() # fails
@receiver(post_save, sender=InstagramMedia, dispatch_uid='create_competition_media2')
def create_competition_media2(sender, instance, created, **kwargs):
CompetitionMedia.objects.create(instagrammedia=instance) # Fails
Is it possible to do this?
I’m apparently supposed to set
parent_ptr, like:However there’s an issue where if you create and save the parent first and then try to create a child, the child will override te parent even on fields you haven’t set in the child. As described in https://code.djangoproject.com/ticket/11618 and https://code.djangoproject.com/ticket/7623, so I would avoid it just because it’s not very obvious that would happen.
If someone really wants to go down that route you’d need to do:
better yet: