Code

Opened 8 years ago

Last modified 2 years ago

#3006 new New feature

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

Reported by: jeroen_at_lbvd.nl 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

Description

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:
        pass

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

    my_field = models.CharField(maxlength=50)

    class Admin:
        pass


Then I expect to be able to do something like:

my_model_instance.security_context.accesslist_set.filter(principal=user_object)

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:

my_model_instance.security_context.accesslist_set.filter(principal_id=user_object.id)

Attachments (1)

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

Download all attachments as: .zip

Change History (18)

comment:1 Changed 7 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 6 years ago by jacob

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

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

  • Cc tom@… added

comment:4 Changed 6 years ago by mrts

  • milestone set to 1.0

In scope for 1.0.

comment:5 Changed 6 years ago by Alex

  • Keywords generic relations added
  • Version set to SVN

comment:6 Changed 6 years ago by rmyers

  • Owner changed from nobody to rmyers

comment:7 Changed 6 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 6 years ago by rmyers

documentation patch added

comment:8 Changed 6 years ago by rmyers

  • Has patch set

comment:9 Changed 6 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 6 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 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:12 Changed 5 years ago by anonymous

  • Cc fridrik@… added

comment:13 Changed 3 years ago by lrekucki

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

comment:14 Changed 3 years ago by anonymous

  • Type changed from Bug to New feature

comment:15 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:16 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:17 Changed 2 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:

https://github.com/coleifer/django-generic-aggregation/blob/master/generic_aggregation/utils.py

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from rmyers to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.