﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36442	Reusing same instance of FilteredRelations in multiple queries result in django.core.exceptions.FieldError: Cannot resolve keyword	Viliam Mihálik	Viliam Mihálik	"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.
"	Bug	closed	Database layer (models, ORM)	5.0	Normal	fixed	FilteredRelation, ORM		Ready for checkin	1	0	0	0	0	0
