Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Generic ForeignKey in Database Queries [RSS Feed]

#1 Dec. 7, 2010 02:24:11

K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generic ForeignKey in Database Queries


I have a model set up thusly:

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

Item(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
object = generic.GenericForeignKey()

If I try to perform queries in the format
Item.objects.filter(object=some-object), I raise a FieldError: "Cannot
resolve keyword 'object' into field". But if I have some instance of
Item and I call item-instance.object, it returns the object correctly.

Is this a behavioral malfunction, or are Generic ForeignKeys not meant
to be used in database queries? If they are not, is there any real
advantage to having one?

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 7, 2010 02:28:58

a.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generic ForeignKey in Database Queries


>From the django doc "Due to the way GenericForeignKey is implemented,
you cannot use such fields directly with filters (filter() and
exclude(), for example) via the database API. They aren't normal field
objects."http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relationsOn Dec 6, 6:24 pm, Katrina <katrina.m.elli...@gmail.com> wrote:
> I have a model set up thusly:
>
> from django.contrib.contenttypes.models import ContentType
> from django.contrib.contenttypes import generic
>
> Item(models.Model):
>     content_type = models.ForeignKey(ContentType)
>     object_id = models.PositiveIntegerField()
>     object = generic.GenericForeignKey()
>
> If I try to perform queries in the format
> Item.objects.filter(object=some-object), I raise a FieldError: "Cannot
> resolve keyword 'object' into field".  But if I have some instance of
> Item and I call item-instance.object, it returns the object correctly.
>
> Is this a behavioral malfunction, or are Generic ForeignKeys not meant
> to be used in database queries?  If they are not, is there any real
> advantage to having one?

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Generic ForeignKey in Database Queries [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support