S
S
s1vemod2021-07-09 18:48:56
Django
s1vemod, 2021-07-09 18:48:56

How to filter by field from queryset?

I customize django-admin like this:

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    search_fields = ['name',  'id']
    list_filter = ('is_active',  'availability')
    
    list_display = ['availability']
    readonly_fields = ['availability']

    def get_queryset(self, request):
        queryset = super(ProductAdmin, self).get_queryset(request)
        return queryset.annotate(availability=Case(
            When(quantity__gt=0, then=True),
            default=False
        ))
    
    def availability(self, obj):
        return 'Да' if obj.availability else 'Нет'
    availability.short_description = 'В наличие'


When I add the availability field to list_filter , Django gives me an error:
<class 'shop.admin.ProductAdmin'>: (admin.E116) The value of 'list_filter[1]' refers to 'availability', which does not refer to a Field.


I tried to add this field to fields , but the error remains.

Tell me how can this be fixed?
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question