It is because you are creating meeting object, before saving "place" in db.
So place object doesn't have any id yet. So test 1 should be:

   place = Place(where='Paris')
   place.save()
   meeting = Meeting(place=place, when='2011-01-01')



   meeting.save()
   return meeting
2011/8/19 Jacco <[email protected]>

> I am having great difficulties in saving nested models in an easy
> way.
>
>
> Consider the following Example :
>
> ----------------------------------------------------------------------------------------------------------------------------------------------
> class Place(models.Model):
>    where   = models.CharField(max_length=10)
> class Meeting(models.Model):
>    place = models.ForeignKey(Place)
>    when  = models.DateField()
>
> #Two (almost) identical functions:
> def test1():
>    place = Place(where='Paris')
>    meeting = Meeting(place=place, when='2011-01-01')
>
>    place.save()
>
>    meeting.save()
>    return meeting
>
> 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 !!!!!!!!!!!!
>    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?
>
> --
> 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.
>
>

-- 
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