Is it possible to set the implicit OneToOne key directly when using multi table inheritance in Django?

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?

Answers


As I understand it, you want to get something like this:

class CompetitionMedia(models.Model):
    instagrammedia = models.OneToOneField(InstagramMedia, primary_key=True)
    visible = models.BooleanField(default=True)
    most_creative = models.BooleanField(default=False)

@receiver(post_save, sender=InstagramMedia, dispatch_uid='create_competition_media2')
def create_competition_media(sender, instance, created, **kwargs):
    if created:  
         CompetitionMedia.objects.create(instagrammedia=instance)

Need Your Help

Expression.Lambda and query generation at runtime, simplest “Where” example

c# .net lambda expression-trees dynamic-language-runtime

I was trying to generate a simple Lambda Expression at runtime with no luck... something like this:

How to not include blank fields when updating a MySQL record

php mysql forms sql-update

How would I not include text fields of a form left blank in a MySQL update query? I understand why it's replacing filled fields with empty strings, but I'm not sure of an efficient way to fix it. I...

Preserve utf8 when loading HTML from file

php encoding utf-8 domdocument

Well, apparently, PHP and it's standard libraries have some problems, and DOMDocument isn't an exception.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.