Hi,

I'm working on a project which makes extensive use of generic
relations. I'm having trouble with lookups across the generic
relation.

Cut-down version of the models:

class Post(models.Model):
    text = models.TextField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    subject = generic.GenericForeignKey()

class Person(models.Model):
    name = models.CharField(max_length=127)
    site = models.ManyToManyField(Site)
    ....
    posts = generic.GenericRelation(Post)

class Organisation(models.Model):
    name = models.CharField(max_length=127)
    site = models.ManyToManyField(Site)
    ....
    posts = generic.GenericRelation(Post)

class Item(models.Model):
    name = models.CharField(max_length=127)
    site = models.ManyToManyField(Site)
    ....
    posts = generic.GenericRelation(Post)

>From an individual Post, I can get the related item's name easily via
the subject attribute:
>>> p=Post.objects.get(pk=1)
>>> p.subject.name
u'Joe Bloggs'

But the problem comes when trying to do lookups from the Post model
via the generic relation, again using the subject attribute:
>>> Post.objects.filter(subject__name="Joe Bloggs")
<snip traceback>
TypeError: Cannot resolve keyword 'subject' into field. Choices are:
organisation, person, item, ... id, text, content_type, object_id

It seems that the "subject" attribute, despite being directly defined
in the Post model, is only available from instances - in lookups, you
can only use the individual models the Post is (generically) related
to.

Now I could change that filter to use the models explicitly -
Post.objects.filter(Q(person__name="Joe Bloggs") |
Q(organisation__name="Joe Bloggs"))
- but that seems to defy the purpose of generic relations, and would
mean having to remember to change the lookup if I added another model.

Am I doing something wrong, or is this a bug in generic relations? If
so, is it one of those that is going to be fixed Real Soon Now in
queryset-refactor?

Thanks for any ideas,
DR.
--~--~---------~--~----~------------~-------~--~----~
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