我正在尝试使用Django的官方教程。具体来说,是modeladmin list_display部分:
http://docs.djangoproject.com/en/1.2/intro/tutorial02/#customize-the-admin-change-list
如何在列表中添加一列,显示每个投票的选项数量?谢谢!
我正在尝试使用Django的官方教程。具体来说,是modeladmin list_display部分:
http://docs.djangoproject.com/en/1.2/intro/tutorial02/#customize-the-admin-change-list
如何在列表中添加一列,显示每个投票的选项数量?您不需要编辑您的模型,可以在ModelAdmin对象中创建一个函数来计算管理器中的列,该函数接受第二个参数,这将是常规的Poll模型实例。然后像在模型中一样编写代码,您可以忽略self,因为它没有您想要的内容。
class PollAdmin(admin.ModelAdmin)
list_display = (<other_fields>, 'choice_count')
def choice_count(self, model_instance):
return model_instance.choice_set.count()
pcount
),用于返回给定 Poll
实例的选项数量。然后,您可以将其添加到您的 ModelAdmin
子类中的 list_display
属性中。class Poll(models.Model):
...
def pcount(self):
return self.choice_set.count()
class PollAdmin(admin.ModelAdmin):
list_display = (<other fields>, 'pcount', )
pcount
方法中添加了缺失的 return
语句。你有注意到吗? - Manoj GovindanPoll
实例返回的pcount
吗? - Manoj Govindan