Right, that's a mistake in the example. I fixed it, but the result is
an empty string:
$ manage.py syncdb --noinput
Installing json fixture 'initial_data' from '/home/phuihock/obnob/
inheritance/animal/fixtures'.
category: ''
Installed 2 object(s) from 1 fixture(s)

Just to be a little be clearer, here's the output from the shell.
$ manage.py shell
Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from animal.models import Dog
>>> Dog.objects.get(pk=1).category
u'omnivore'

On Apr 8, 7:14 pm, Matthias Kestenholz <matthias.kestenh...@gmail.com>
wrote:
> On Thu, Apr 8, 2010 at 1:04 PM, Phui Hock <phuih...@gmail.com> wrote:
> > Given the following block of code:
> > --- models.py ---
> > from django.db import models
> > from django.db.models.signals import post_save
>
> > class Animal(models.Model):
> >    category = models.CharField(max_length=20)
>
> > class Dog(Animal):
> >    color = models.CharField(max_length=10)
>
> > def echo_category(sender, **kwargs):
> >    print "category: '%s'" % sender.category
>
> > post_save.connect(echo_category, sender=Dog)
>
> > and the following fixture:
> > --- initial_data.json ---
> > [
> > {
> >    "pk": 1,
> >    "model": "animal.animal",
> >    "fields": {
> >        "category": "omnivore"
> >    }
> > },
> > {
> >    "pk": 1,
> >    "model": "animal.dog",
> >    "fields": {
> >        "color": "brown"
> >    }
> > }
> > ]
>
> > Executing manage.py syncdb throws "AttributeError: type object 'Dog'
> > has no attribute 'category'" from inside of echo_category callback.
> > This is weird because I can access category from the Dog instance if
> > it is an instance from a query, say Dog.objects.get(pk=1).
>
> > How come the Dog instance received by echo_category is different
> > compared to the Dog instance from a query? Can't I do post_save on a
> > sub-model?
>
> Please note that sender != instance. The Dog type object (=class) does
> not have a category instance itself; a Dog instance however does have
> one, since it is inherited from Animal.
>
> You should change your signal handler to something like this:
>
> def echo_category(sender, instance, **kwargs):
>     print u"category: %s" % instance.category
>
> --
> FeinCMS Django CMS building toolkit:http://spinlock.ch/pub/feincms/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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