[Fixed]-How can I create a new Django instance using reverse-foreign key without saving it to DB?

1👍

If I understand what you mean .. you want something like a.MyModelB_a.new(my_int=6). Something like .build in rails for example! I’m afraid that’s not exist in django.

But if you just don’t want to import MyModelB for some reason, you can use a work around if you are OK with that.

You can use a.MyModelB_a.model(my_int=6, my_a=a)

Edit:

Or you can override Manager class to implement your own method. I didn’t try to override RelatedManager before, but it appears that it’s allowed in django by use_for_related_fields.

👤Moamen

Leave a comment