Opened 2 months ago

Closed 2 months ago

#35282 closed Bug (duplicate)

ProgrammingError when admin.ShowFacets.ALWAYS and filter in list_filter of type SimpleListFilter on search.

Reported by: dz Owned by: nobody
Component: contrib.admin Version: 5.0
Severity: Normal Keywords: SimpleListFilter
Cc: dz Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (2)

comment:1 by dz, 2 months ago

Upgrading from django-5.0.2 to django-5.0.3, resolves the issue.

comment:2 by Natalia Bidart, 2 months ago

Keywords: ShowFacets removed
Resolution: duplicate
Status: newclosed
Type: UncategorizedBug

Duplicate of #35198.

Note: See TracTickets for help on using tickets.
Back to Top