3👍
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()
1👍
Add a custom method (say pcount
) that returns the number of choices for a given Poll
instance. You can then add this to the list_display
attribute in your ModelAdmin
subclass.
class Poll(models.Model):
...
def pcount(self):
return self.choice_set.count()
class PollAdmin(admin.ModelAdmin):
list_display = (<other fields>, 'pcount', )
- [Django]-How to correctly format a datetime with Django and Python
- [Django]-Django Rest Framework Filtering an object in get_queryset method
Source:stackexchange.com