django-polymorphic
django-polymorphic copied to clipboard
Query with FilteredRelation
Hi! I tried using Django FilteredRelation with a PolymorphicQueryset and I got an error https://docs.djangoproject.com/en/3.1/ref/models/querysets/#filteredrelation-objects
Some PolymorphicModel:
from django.db import models
from polymorphic.models import PolymorphicModel
class ModelA(PolymorphicModel):
comment = models.CharField(max_length=255, blank=True)
class ModelB(ModelA):
modelC = models.ForeignKey('ModelC', on_delete=models.CASCADE)
Basic query with FilteredRelation:
from django.db.models import FilteredRelation, Q
ModelB.objects.annotate(test=FilteredRelation('modelC', condition=Q()))
Got this error:
File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/polymorphic/query.py", line 307, in annotate
self._process_aggregate_args(args, kwargs)
File "/usr/local/lib/python3.7/site-packages/polymorphic/query.py", line 302, in _process_aggregate_args
patch_lookup(a)
File "/usr/local/lib/python3.7/site-packages/polymorphic/query.py", line 273, in patch_lookup
a.name = translate_polymorphic_field_path(self.model, a.name)
AttributeError: 'FilteredRelation' object has no attribute 'name'