Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django modeladmin list_display

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!

like image 792
M.A. Avatar asked Nov 24 '25 16:11

M.A.


1 Answers

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()
like image 98
Lincoln B Avatar answered Nov 27 '25 06:11

Lincoln B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!