Django modeladmin list_display

I am trying to play with the official Django tutorial. Specifically, modeladmin list_display:

http://docs.djangoproject.com/en/1.2/intro/tutorial02/#customize-the-admin-change-list

How to add a column that displays the number of options for each survey in the list?

Thank!

+3
source share
2 answers

You do not need to edit your model to calculate columns for the administrator on the fly, creating a function in the ModelAdmin object that accepts the second parameter, this will be a regular instance of the polling model. By writing code the same way as in the model, you can ignore yourself here, since it does not 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()
+3
source

( pcount), Poll. list_display ModelAdmin.

class Poll(models.Model):
    ...
    def pcount(self):
        return self.choice_set.count()


class PollAdmin(admin.ModelAdmin):
    list_display = (<other fields>, 'pcount', )
+1

Source: https://habr.com/ru/post/1761956/


All Articles