我正在尝试使用Django的官方教程。特别是modeladmin list_display:
http://docs.djangoproject.com/en/1.2/intro/tutorial02/#customize-the-admin-change-list
如何添加一列显示列表中每个投票的选项数?
谢谢!
答案 0 :(得分:3)
您无需编辑模型,即可计算管理员的动态列,在ModelAdmin对象中创建一个带有第二个参数的函数,这将是常规的Poll模型实例。比起在模型中编写代码,你可以在这里忽略自己,因为它没有你想要的东西。
class PollAdmin(admin.ModelAdmin)
list_display = (<other_fields>, 'choice_count')
def choice_count(self, model_instance):
return model_instance.choice_set.count()
答案 1 :(得分:1)
添加一个自定义方法(比如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', )