On Fri, Mar 20, 2009 at 6:28 PM, Brian McKeever <kee...@gmail.com> wrote:

>
> What am I doing wrong?
> Is there any other way to do what I want?
> I'm basically trying to implement the GOF's strategy design pattern
> (the real code is more complicated than this example).
>
> ## my models
> class Donkey(models.Model):
>        name = models.CharField(max_length=11)
>
> class Barn( models.Model):
>        content_type = models.ForeignKey(ContentType)
>        object_id = models.PositiveIntegerField()
>        animal = generic.GenericForeignKey('content_type', 'object_id')
>
>
> ##my test
> def test_barn(self):
>                donkey = Donkey( name = "Stewart")
>                donkey.save()
>                barn = Barn(animal = donkey)
>                barn.save()
>                self.assertEqual( barn.animal.name, "Stewart")
>                donkey.name = "Bob"
>                donkey.save()
>                self.assertEqual( len(Donkey.objects.all()),1)
>                self.assertEqual( Donkey.objects.all()[0].name, "Bob")
>                self.assertEqual( barn.animal.name, "Bob")
>                #this fails saying: AssertionError: u'Stewart' != 'Bob'
>
> >
>
When you get a related object using the accessor it is cached for the
lifetime of that obj so you don't need to requery for each attribute(as that
would be hugely expensive).  So in essence your test is showing this
behavior.  To fix the test requery for barn.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

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

Reply via email to