Answer the question
In order to leave comments, you need to log in
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 = 'В наличие'
<class 'shop.admin.ProductAdmin'>: (admin.E116) The value of 'list_filter[1]' refers to 'availability', which does not refer to a Field.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question