Correct, because a foreign key is related to an object by its primary key - which doesn’t exist until the related object has been saved. This is by design and is exactly as it should be.
You can save them both at effectively the same time by ensuring the two statements are within the same transaction.
I have to say I’m with @kaypee90 on this. If you have created the related object, and set the relevant element in the parent object to that value, it is logical to think/hope that the ORM recognises that the child object does not have an id, or has been changed, and therefore needs to be saved when the parent is saved.
That’s independent of the SQL needed to do all of that.
This really isn’t an opinion-type issue here, I’m trying to explain how the Django ORM works. It is what it is.
When you try to save an object to the database (which is what the save method does), it must create a valid row - the database isn’t going to accept anything that isn’t valid.
And the design of the ORM is such that it doesn’t try to resolve related objects automatically.
<opinion>
Yes, an argument could be made that it would be “better” if it did. Fortunately, the design of Django itself allows you to implement that by overriding the save method to examine related objects and perform those operations should you choose to handle it that way.
Personally, I think trying to implement that globally would be an incredibly bad idea because of all the edge-cases and unusual situations that I could see occurring. I think the work necessary to solve the general case far exceeds any benefit that may be obtained.