Opened 9 years ago

Last modified 3 years ago

#3006 new New feature

generic relations do not act as expected in a filter/get

Reported by: Owned by: rmyers
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: generic relations
Cc: tom@…, fridrik@… Triage Stage: Someday/Maybe
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


If I have a model like this:

class SecurityContext(models.Model):
    title = models.CharField(maxlength=100)
    description = models.TextField()

class AccessList(models.Model):
    principal_id = models.PositiveIntegerField()
    principal_ct = models.ForeignKey(ContentType)

    security_context = models.ForeignKey(SecurityContext)
    principal = models.GenericForeignKey(fk_field="principal_id", ct_field="principal_ct")
    permission = models.ForeignKey(Permission)
    class Admin:

class MyModel(models.Model):
    security_context = models.ForeignKey(SecurityContext)
    owner = models.ForeignKey(User)

    my_field = models.CharField(maxlength=50)

    class Admin:

Then I expect to be able to do something like:


and have it return a list of all accesslist entries for the given principal (similar to ForeignKeys or M2M relations).
Instead I get an error stating: Cannot resolve keyword 'principal' into field.

The following does work however:


Attachments (1)

3006.patch (638 bytes) - added by rmyers 7 years ago.
documentation patch added

Download all attachments as: .zip

Change History (18)

comment:1 Changed 8 years ago by anonymous

If nothing can be done to fix this, the fact that it cannot be done should at least be documented.

comment:2 Changed 7 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted
  • Version SVN deleted

comment:3 Changed 7 years ago by Thomas Steinacher <tom@…>

  • Cc tom@… added

comment:4 Changed 7 years ago by mrts

  • milestone set to 1.0

In scope for 1.0.

comment:5 Changed 7 years ago by Alex

  • Keywords generic relations added
  • Version set to SVN

comment:6 Changed 7 years ago by rmyers

  • Owner changed from nobody to rmyers

comment:7 Changed 7 years ago by rmyers

I believe this functionality will not be easy to create. Basically a generic foreign key is not a field, so it can not be used in a the ORM like normal fields. It would need to be coverted to a field subclass and all the methods that work with the db will need to be overridden. I question whether it would be a good idea to make this field overly complicated just to get this functionality.

Changed 7 years ago by rmyers

documentation patch added

comment:8 Changed 7 years ago by rmyers

  • Has patch set

comment:9 Changed 7 years ago by mtredinnick

  • milestone changed from 1.0 to post-1.0
  • Triage Stage changed from Accepted to Someday/Maybe

It's probably possible to do this eventually, although it needs #5937 to be fixed first. Agree that it's fiddly, though. Marking for post-1.0. It's never been reasonable to expect this to work. We can make this someday/maybe in case somebody comes up with a good implementation in the future.

I'll drop in your docs patch in a moment to clarify things for 1.0. Thanks for doing the investigative work on this one.

comment:10 Changed 7 years ago by mtredinnick

(In [8417]) Documented that GenericForeignKey fields can't be used transparently in
filters. Refs #3006. Patch from rmyers.

comment:11 Changed 6 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:12 Changed 6 years ago by anonymous

  • Cc fridrik@… added

comment:13 Changed 4 years ago by lrekucki

  • Severity changed from normal to Normal
  • Type changed from defect to Bug

comment:14 Changed 4 years ago by anonymous

  • Type changed from Bug to New feature

comment:15 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:16 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:17 Changed 3 years ago by coleifer

I'd like to work up a patch to merge in some of the functionality i've written for dealing with GFKs -- including filter, annotate and aggregate:

Note: See TracTickets for help on using tickets.
Back to Top