#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.

-- 
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/010701974125aba5-28f03927-1b9e-4279-85ff-e278f8bbdfdb-000000%40eu-central-1.amazonses.com.

Reply via email to