[Answer]-Django ForeignKey auto field

2👍

Couldn’t you do something like this?

def save(self, *args, **kwargs):
    if not self.belongs_to:
        self.belongs_to = self.related_email.belongs_to
    return super(Blog, self).save(*args, **kwargs)

-1👍

First, add null = True to Blog.belongs_to

Assuming that you know the owner of the registration email, you can do

owner = User.objects.get(field = value)
email = Email.objects.get(belongs_to = owner)
Blog.objects.create(address = ..., related_email = email, belongs_to = None)

And then should you need to do any updates, you can do

blog = Blog.objects.filter(related_email = email).filter(belongs_to__isnull = True)
blog.belongs_to = newUserObject
👤Jason

Leave a comment