Hi Malcolm,
After reading your post, I added a couple of assertions.
1) Check for subProject.parent immediately after creating the
subProject object.
2) Compare id()s of project and subProject.parent *after saving*
subProject.

The test now looks like this:

class ProjectTests(unittest.TestCase):
    def testCreateProjectAndSubProject(self):
        project = Project(name = "aProject")
        subProject = Project(name = "aSubProject", parent = project)

        self.assert_(subProject.parent) # Passes

        project.save()
        self.assert_(project.id) # Passes

        subProject.save()
        self.assertEqual(id(project), id(subProject.parent)) # Fails

        self.assert_(subProject.parent) # Fails

The first new assertion worked, while the second failed.  It seems
that something weird is happening somewhere inside Django. It would be
greatly appreciated if one (or more) of the developers could please
have a look at this and tell me if and where I am going wrong.

Thanks,
Manoj


On Feb 25, 9:18 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Sat, 2007-02-24 at 18:41 +0000, Manoj Govindan wrote:
> > Hi,
> > Consider a model and a simple test for the model.
>
> > class Project(models.Model):
> >     name = models.CharField(maxlength = 255)
> >     parent = models.ForeignKey('self', null = True)
>
> > class ProjectTests(unittest.TestCase):
> >     def testCreateProjectAndSubProject(self):
> >         project = Project(name = "aProject")
> >         subProject = Project(name = "aSubProject", parent = project)
>
> >         project.save()
> >         self.assert_(project.id) # Passes
>
> >         subProject.save()
> >         self.assert_(subProject.parent) # Fails
>
> > It seems that even though the Parent object gets saved and has an id
> > (see the line in the test with the comment "Passes") the parent object
> > is None when I access it through the child. It seems that parent and
> > child.parent point to two different objects even though the original
> > parent was supplied to the child's constructor.
>
> You should be able to test that last claim by looking at id(project) and
> id(subProject.parent) to see if that is the case (your final assert
> suggests that it was never assigned and hence is None, not that a new
> instance was created). Is suspect you'll find they are the same instance
> at the Python level, but something odd is going on when the saving
> occurs, but I haven't tested this (just thinking of how Django handles
> field attributes). You could also test the contents of subProject.parent
> prior to the save() call to help narrow down where the mystery takes
> place, too.
>
> One change to try would be to ensure you save the project instance
> first, before trying to refer to it.
>
> I don't have time to delve too deeply into this problem this afternoon,
> but it would be interesting to try and work out what is going on here.
>
> Regards,
> Malcolm


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