#32239: FilteredRelation is not working with GenericRelation
-------------------------------------+-------------------------------------
     Reporter:  Omar Altayyan        |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  3.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:  FilteredRelation     |             Triage Stage:
  GenericRelation Generic Relation   |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Ammar-Shiekh):

 Here is the `test/models.py` file:


 {{{
 from django.db import models


 class Workspace(models.Model):
     pass

 class Event(models.Model):
     workspace = models.ForeignKey(Workspace, on_delete=models.CASCADE)

     limit = models.FloatField(default=0)

     entity_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
     entity_id = models.PositiveIntegerField()
     entity = GenericForeignKey('entity_type', 'entity_id')

 class Entity(models.Model):
     events = GenericRelation(Event, content_type_field='entity_type',
 object_id_field='entity_id')

     class Meta:
         abstract = True

 class SomeEntity(Entity):
     pass
 }}}

 After migrating these models to the database, try running the following
 code using `python manage.py shell`:

 {{{
 >>> from django.db.models import FilteredRelation
 >>> from django.db.models.functions import Coalesce
 >>> from test.models import SomeEntity
 >>> str(SomeEntity.objects.annotate(
 ...     workspace_events=FilteredRelation('events',
 condition=Q(events__workspace_id=1)),
 ...     limit=Coalesce('workspace_events__limit', 0),
 ... ).values('id', 'workspace_events__id',
 'limit').order_by('-limit').query)
 }}}

 The last command will print this:

 {{{
 'SELECT "test_someentity"."id", workspace_events."id",
 COALESCE(workspace_events."limit", 0) AS "limit" FROM "test_someentity"
 LEFT OUTER JOIN "test_event" workspace_events ON ("test_someentity"."id" =
 workspace_events."entity_id" AND (workspace_events."entity_type_id" =
 186)) ORDER BY "limit" DESC'
 }}}

 Notice that the condition on events workspace id specified by the
 FilteredRelation doesn't have an effect on the resulted query.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32239#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.6d9da861ba5757ddb30d13773333848b%40djangoproject.com.

Reply via email to