Understanding Django Model Relationships
Why Stack Overflow recommends it
Stack Overflow users link it when answering or asking about Django foreign keys, reverse relationships and the NOT NULL error from saving a child with an unsaved parent.
Recommended by 2 people across 2 different months, 2019 to 2022.
What it is
A roughly 26-minute tutorial from the Pretty Printed channel on defining and querying relationships between Django models, including foreign keys and reverse lookups. It walks through examples with a Language and Framework model, including the error you get when saving a child before its parent.
What people said
Recommends abautista · 2020-03 · Stack Overflow · on “Setting up a foreign key relationship in Django using postgreSQL”
Looking at some old questions such as this one [LINK] and this tutorial [this video] I will be implementing your changes now.
Recommends Tanveer · 2022-11 · Stack Overflow · on “Get First and Last Related Object for Django Rest API”
oh got it. first, you will get it using the reverse relationship..! and the second one is using serializermethodfield.. [this video] you will get it reverse relationship example from that link. if you didn't get it let me know.
Cites FAM_Maurice · 2019-12 · Stack Overflow · on “Why is an unsaved parent class instance causing a NOT NULL constraint error when saving the child instance?”
I'm following a tutorial of Pretty printed on youtube. During the tutorial he tries to instantiate a child model instance with a unsaved parent model instance called Language . But when he runs the code he gets an IntegrityError: NOT NULL constraint failed: example_framework.language_id The narrator then explains that this is because he didn't call save() on the parent instance first leaving it without a primary key. This is the code form the tutorial, and heres the youtube part where he encounters the error: [this video] java = Language(name='Java') spring = Framework(name='Spring', language=java) java.save() spring.save() I don't understand why this gives an error though. Doesn't spring referen