I'm trying to play with the Django's official tutorial. Specifically the modeladmin list_display:
http://docs.djangoproject.com/en/1.2/intro/tutorial02/#customize-the-admin-change-list
How can I add a column that displays the number of choices for each poll in the list?
Thanks!
You don't need to edit your model, to calculate columns for admin on the fly create a function in the ModelAdmin object that takes a second param, this will be the regular Poll model instance. Than write code just like you would in a model, you can ignore the self here since it doesn't have what you want.
class PollAdmin(admin.ModelAdmin)
list_display = (<other_fields>, 'choice_count')
def choice_count(self, model_instance):
return model_instance.choice_set.count()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With