[Django] #36442: Reusing same instance of FilteredRelations in multiple queries result in django.core.exceptions.FieldError: Cannot resolve keyword

12 views
Skip to first unread message

Django

unread,
Jun 5, 2025, 1:31:25 PMJun 5
to django-...@googlegroups.com
#36442: Reusing same instance of FilteredRelations in multiple queries result in
django.core.exceptions.FieldError: Cannot resolve keyword
-------------------------------------+-------------------------------------
Reporter: Viliam Mihálik | Type:
| Uncategorized
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: FilteredRelation, | Triage Stage:
ORM | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Example test case that should pass, used in FilteredRelationTests

{{{
def test_reuse_same_filtered_relation(self):
borrower = Borrower.objects.create(name="Jenny")
Reservation.objects.create(
borrower=borrower,
book=self.book1,
state=Reservation.STOPPED,
)
condition = Q(book__reservation__state=Reservation.STOPPED)
my_reserved_books = FilteredRelation(
"book__reservation",
condition=condition
)
first_result = list(Author.objects.annotate(
my_reserved_books=my_reserved_books,
))
self.assertEqual(my_reserved_books.condition, condition)
# AssertionError: <Q: (AND: ('my_reserved_books__state', 'stopped'))>
!= <Q: (AND: ('book__reservation__state', 'stopped'))>
second_result = list(Author.objects.annotate(
my_reserved_books=my_reserved_books,
))
# django.core.exceptions.FieldError: Cannot resolve keyword
'my_reserved_books' into field. Choices are: book, content_object,
content_type, content_type_id, favorite_books, id, name, object_id
}}}


When you reuse same `FilteredRelation` in different queries, internal
`FilteredRelation.condition` is changed and no longer works.
This same code works on Django 4.x. This change was introduced by fixing
of bug #33766

Based on discussion in #33766 it was intended but breaking for us after
upgrading from 4 -> 5.
In order to get this working is to `my_reserved_books.clone()` clone
annotate before using.
--
Ticket URL: <https://code.djangoproject.com/ticket/36442>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 5, 2025, 5:06:04 PMJun 5
to django-...@googlegroups.com
#36442: Reusing same instance of FilteredRelations in multiple queries result in
django.core.exceptions.FieldError: Cannot resolve keyword
-------------------------------------+-------------------------------------
Reporter: Viliam Mihálik | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: FilteredRelation, | Triage Stage: Accepted
ORM |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted
* type: Uncategorized => Bug
* version: 5.2 => 5.0

Comment:

We should definitely clone the filtered relation on resolving, the bug is
present since 5.0 though so it doesn't qualify for a backport.

Would you be interested in submitting a patch here? I suspect adjusting
`FilteredRelation.clone` to make sure it passes
`condition=self.condition.clone()` should do?
--
Ticket URL: <https://code.djangoproject.com/ticket/36442#comment:1>

Django

unread,
Jun 5, 2025, 5:27:39 PMJun 5
to django-...@googlegroups.com
#36442: Reusing same instance of FilteredRelations in multiple queries result in
django.core.exceptions.FieldError: Cannot resolve keyword
-------------------------------------+-------------------------------------
Reporter: Viliam Mihálik | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: FilteredRelation, | Triage Stage: Accepted
ORM |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Viliam Mihálik):

I can look at it.
I checked the code, and after debug, .clone method is never called. So my
suggestion is to clone annotation before "rename" here
https://github.com/django/django/blob/main/django/db/models/query.py#L1728

and simply change `clone.query.add_filtered_relation(annotation, alias)`
to `clone.query.add_filtered_relation(annotation.clone(), alias)`
--
Ticket URL: <https://code.djangoproject.com/ticket/36442#comment:2>

Django

unread,
Jun 5, 2025, 5:53:56 PMJun 5
to django-...@googlegroups.com
#36442: Reusing same instance of FilteredRelations in multiple queries result in
django.core.exceptions.FieldError: Cannot resolve keyword
-------------------------------------+-------------------------------------
Reporter: Viliam Mihálik | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: FilteredRelation, | Triage Stage: Accepted
ORM |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Viliam Mihálik):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36442#comment:3>

Django

unread,
Jun 6, 2025, 3:30:35 AMJun 6
to django-...@googlegroups.com
#36442: Reusing same instance of FilteredRelations in multiple queries result in
django.core.exceptions.FieldError: Cannot resolve keyword
-------------------------------------+-------------------------------------
Reporter: Viliam Mihálik | Owner: Viliam
| Mihálik
Type: Bug | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: FilteredRelation, | Triage Stage: Accepted
ORM |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* owner: (none) => Viliam Mihálik
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36442#comment:4>

Django

unread,
Jun 11, 2025, 9:01:15 AMJun 11
to django-...@googlegroups.com
#36442: Reusing same instance of FilteredRelations in multiple queries result in
django.core.exceptions.FieldError: Cannot resolve keyword
-------------------------------------+-------------------------------------
Reporter: Viliam Mihálik | Owner: Viliam
| Mihálik
Type: Bug | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: FilteredRelation, | Triage Stage: Ready for
ORM | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36442#comment:5>

Django

unread,
Jun 12, 2025, 2:37:06 AMJun 12
to django-...@googlegroups.com
#36442: Reusing same instance of FilteredRelations in multiple queries result in
django.core.exceptions.FieldError: Cannot resolve keyword
-------------------------------------+-------------------------------------
Reporter: Viliam Mihálik | Owner: Viliam
| Mihálik
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: FilteredRelation, | Triage Stage: Ready for
ORM | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"bd65e82831304ede92af6e9f0807daa3a874efc0" bd65e828]:
{{{#!CommitTicketReference repository=""
revision="bd65e82831304ede92af6e9f0807daa3a874efc0"
Fixed #36442 -- Cloned FilteredRelation before rename_prefix_from_q.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36442#comment:6>
Reply all
Reply to author
Forward
0 new messages