Tuesday, June 28, 2011

Re: admin search - lookup following GenericForeignKey

> I have a model X with Y field which is GenericForeignKey field. In the
> model admin for model X I want to have something like:
> search_fields('Y__fieldname'). I get the error 'Cannot resolve keyword
> "X" into field' which makes sense since it's a virtual field and
> doesn't go to fields in X._meta
> Any ideas? Thanks...

Following GFKs forwards in lookups isn't supported at all.
The reason is pretty simple: you don't have any guarantees about the
object on the other side of the relation. You wouldn't even know
against which table you want to join.

There is a note in the docs [1], see the last paragraph of this

To make this work, we'd have to add some kind of mechanism to specify
the content type against which the join should happen but there's no
such thing at the moment. If you have any idea on what this should
look like, I'd gladly look into this. (-;


[1] https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#django.contrib.contenttypes.generic.GenericForeignKey

No comments:

Post a Comment