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