Friday, August 19, 2011

Strange behavior in model.save()

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 django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate