Django - reference method in the list_display tuple for the admin


Django tip:

Besides model fields, the list_display tuple can reference methods from ModelAdmin:

@admin.register(ShoppingList)
class ShoppingListAdmin(admin.ModelAdmin):
    list_display = ("title", "number_of_items")

    def number_of_items(self, obj):
        result = ShoppingItem.objects.filter(shopping_list=obj.id).count()
        return result