#35198: Facet filters crash for queryset with no ID.
-------------------------------------+-------------------------------------
     Reporter:  Simon Alef           |                    Owner:  Shafiya
                                     |  Adzhani
         Type:  Bug                  |                   Status:  assigned
    Component:  contrib.admin        |                  Version:  5.0
     Severity:  Release blocker      |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Thomas Feldmann):

 Yes, kind of. The full filter is something like this:


 {{{#!python
 class RentalItemsFilter(admin.SimpleListFilter):
     title = "State"
     parameter_name = "rental_items_state"

     def lookups(self, request, model_admin):
         return [
             ("ACTIVE", "Active"),
             ("CANCELLED", "Cancelled"),
         ]

     def queryset(self, request, queryset):
         if not self.value():
             return queryset

         queryset = queryset.annotate(
             active_rental_item_count=Count(
                 "contracts__rental_items",
 filter=Q(contracts__rental_items__cancel_date__isnull=True),
                 distinct=True,
             ),
             cancelled_rental_item_count=Count(
                 "contracts__rental_items",
 filter=Q(contracts__rental_items__cancel_date__isnull=False),
                 distinct=True,
             ),
         )
         match self.value():
             case "ACTIVE":
                 return queryset.filter(
                     active_rental_item_count__gt=0,
 cancelled_rental_item_count=0
                 )
             case "CANCELLED":
                 return queryset.filter(
                     active_rental_item_count=0,
 cancelled_rental_item_count__gt=0
                 )
             case _:
                 raise ValueError(f"Unknown value {self.value()}")
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35198#comment:7>
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/0107018dbbcefcfe-6d9fbabd-ca36-4a2f-94e4-552931b7b255-000000%40eu-central-1.amazonses.com.

Reply via email to