On 08/19/2011 03:15 PM, Jacco wrote:
I am having great difficulties in saving nested models in an easy
way.


#Two (almost) identical functions:
def test1():
     place = Place(where='Paris')

place is not saved, thus it does not have a primary key (id)

     meeting = Meeting(place=place, when='2011-01-01')

You assign non saved instance to a meeting. Probably what happened internally is that place.pk is copied to meeting.place_id.

     place.save()

You saved place. Changes in model are not reflected to referred model.

     meeting.save()

You save meeting with still (partially) stale place pk. thus resulting an exception.

def test2():
     place = Place(where='Paris')
     meeting = Meeting(place=place, when='2011-01-01')

     place.save()
     meeting.place = meeting.place      #BY setting meeting.place to
itself, it works !!!!!!!!!!!!

You updated your meeting internal structure.
     meeting.save()
     return meeting
----------------------------------------------------------------------------------------------------------------------------------------------



- Running test1() results in crash "null value in column "place_id"
violates not-null constraint"
- Running test2() results in OK.
- Only difference is the dummy line "meting.place = meeting.place".

Could someone who really understands this explain what is going on?


What's going on that you have done it incorrect order.

place = Place(where='Paris')
place.save() # Place saved, PK assigned.

meeting = Meeting(place=place, when='2011-01-01')
meeting.save() # Meeting saved with correct place.

--

Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to