I encountered this error at Django lecture pt 1 8- Providing Links to Other Pages (5:16)
where in the django admin, you can click products count in the collection page to go to the collection with products.
Here are my code-along code in the admin.py and I don’t see the difference from Mosh’s. Thank you in advance.
@admin.register(models.Collection)
class CollectionAdmin(admin.ModelAdmin):
list_display = [‘title’, ‘products_count’]
@admin.display(ordering='products_count')
def products_count(self, collection):
url = (
reverse('admin:store_product_changelist')
+ '?'
+ urlencode({
'collection__id': str(collection.id)
}))
return format_html('<a href="{}">{}</a>',url, collection.products_count)
def get_queryset(self, request: HttpRequest) -> QuerySet[Any]:
return super().get_queryset(request).annotate(
products_count = Count('product')
)