#35282: ProgrammingError when admin.ShowFacets.ALWAYS and filter in list_filter 
of
type SimpleListFilter on search.
-------------------------------------+-------------------------------------
               Reporter:  zaterio    |          Owner:  nobody
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:             |        Version:  5.0
  contrib.admin                      |       Keywords:  ShowFacets
               Severity:  Normal     |  SimpleListFilter
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Hello,

 admin configuration:
 {{{
 class EstadosListFilter(admin.SimpleListFilter):
     title = _("Estados Licitación")
     parameter_name = "cantidad_estados"

     def lookups(self, request, model_admin):
         return [
             ("1", _("1")),
             ("2", _("2")),
         ]

     def queryset(self, request, queryset):
         if self.value() == "1":
             return queryset.filter(_cantidad_estados=1)
         elif self.value() == "2":
             return queryset.filter(_cantidad_estados=2)
         return queryset


 @admin.register(Licitaciones)
 class LicitacionesAdmin(admin.ModelAdmin):
     ordering = ["created_at"]

     exclude = ['user', 'file_path']

     list_display = ['CodigoExterno', 'empresa__NombreEmpresa', 'date',
                     'CodigoEstado', 'FechaCierre', 'Nombre',
 'cantidad_productos', 'cantidad_items',]

     search_fields = ["CodigoExterno", 'empresa__NombreEmpresa',
 "productos__NombreProducto",
                      'licitacion_proveedores__NombreProveedor']

     inlines = [ProveedoresInline, LicitacionesDetallesActiveInline,
                LicitacionesDetallesNoActiveInline, ItemsActiveInline,
 ItemsNoActiveInline]

     list_filter = (
         "CodigoEstado", ('date', admin.DateFieldListFilter),
 EstadosListFilter)

     show_facets = admin.ShowFacets.ALWAYS

     def get_queryset(self, request):
         cantidad_productos_subq =
 Licitaciones.objects.filter(CodigoExterno=OuterRef(
 
"CodigoExterno")).annotate(__cantidad_productos=Count('productos')).values('__cantidad_productos')

         cantidad_estados_subq =
 Licitaciones.objects.filter(CodigoExterno=OuterRef("CodigoExterno")).annotate(
 __cantidad_estados=Count('licitacion_detalle')).values('__cantidad_estados')

         cantidad_items_subq =
 Licitaciones.objects.filter(CodigoExterno=OuterRef("CodigoExterno")).annotate(
             __cantidad_items=Count('licitacion_items',
 filter=Q(licitacion_items__active=True))).values('__cantidad_items')

         ''' solo muestra licitaciones a las cuales se les ha traido el
 detallle'''
         qs = super(LicitacionesAdmin, self).get_queryset(
             request).filter(licitacion_detalle__isnull=False)

         qs = qs.annotate(
             _cantidad_productos=Subquery(cantidad_productos_subq),
             _cantidad_estados=Subquery(cantidad_estados_subq),
             _cantidad_items=Subquery(cantidad_items_subq)
         )
         return qs

     def cantidad_productos(self, obj):
         return obj._cantidad_productos
     cantidad_productos.short_description = 'Productos'
     cantidad_productos.admin_order_field = '_cantidad_productos'

     def cantidad_items(self, obj):
         return obj._cantidad_items
     cantidad_items.short_description = 'Items'
     cantidad_items.admin_order_field = '_cantidad_items'

     def cantidad_estados(self, obj):
         return obj._cantidad_estados
     cantidad_estados.short_description = 'Estados'
     cantidad_estados.admin_order_field = '_cantidad_estados'

     def empresa__NombreEmpresa(self, obj):
         return obj.empresa.NombreEmpresa
     empresa__NombreEmpresa.short_description = 'Razón Social'
 }}}

 At this point, LicitacionesAdmin, correctly displays EstadosListFilter and
 its number of facets in change_list view. But when performing a search, it
 throws the following error:

 {{{
 ProgrammingError at /admin/mpublico/licitaciones/

 invalid reference to FROM-clause entry for table "mpublico_licitaciones"
 LINE 1: ...= U1."licitacion_id") WHERE U0."CodigoExterno" = ("mpublico_...
                                                              ^
 HINT:  Perhaps you meant to reference the table alias "u0".
 }}}

 But changing to:

 {{{
 show_facets = admin.ShowFacets.NEVER
 }}}

 Performs the search without any errors

 On the other hand, when the option is set to ALLOW,  search also works,
 the error in this case appears when clicking on show facets
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35282>
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/0107018e19eb96c6-4744336c-5545-4f82-b38e-0dd417ab0242-000000%40eu-central-1.amazonses.com.

Reply via email to