Saving related model objects

Posted by iHeartDucks on Stack Overflow See other posts from Stack Overflow or by iHeartDucks
Published on 2010-05-09T18:12:47Z Indexed on 2010/05/09 18:18 UTC
Read the original article Hit count: 252

Filed under:
|

I have two related models (one to many) in my django app and When I do something like this

ObjBlog = Blog()
objBlog.name = 'test blog'

objEntry1 = Entry()
objEntry1.title = 'Entry one'

objEntry2 = Entry()
objEntry2.title = 'Entry Two'

objBlog.entry_set.add(objEntry1)
objBlog.entry_set.add(objEntry2)

I get an error which says "null value in column and it violates the foreign key not null constraint".

None of my model objects have been saved. Do I have to save the "objBlog" before I could set the entries? I was hoping I could call the save method on objBlog to save it all.

NOTE: I am not creating a blog engine and this is just an example.

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models