Hi Malcolm,

> I would like to see if subProject.parent the first time you test it
> (your first assert()) is exactly the same as after the subProject.save()
> line.
>

I did a bit of debugging and uncovered the following facts. I think
they explain the goings on pretty clearly.

Let us take the step where subProject instance is created
          subProject = Project(name = "aSubProject", parent =
project)

Remember that at this point, parent object *does not* have an id, as
it has not yet been saved in the DB.
During the process of creating the subProject object (in
models.Model.__init__) django iterates through the list of fields of
the model. The value of each field is automatically popped from the
dictionary of keyword arguments supplied through the constructor. Thus
after the first run, "name" gets assigned as "aSubProject".

The logic differs a little bit the next time around when django
realizes that  "parent" represents a ManyToOne Relationship. It gets
the FK reference from the parent object, that turns out to be the "id"
attribute. As parent has no "id" yet, this turns out to be None.
Nevertheless, this value is set as the "parent_id" attribute of
subProject object. The parent object itself is discarded. At this
point, subProject has a parent attribute, but its value is None.

Thus the following assertion invoked immediately after creating
subProject will pass:
         self.assertEqual(None, subProject.parent)

The obvious work around is to save the parent project before creating
the subProject. Another way would be to set subProject.parent *again*
after the parent project is saved.

Regards,
Manoj


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to