Django modeladmin list_display
我正在尝试使用 Django 的官方教程。具体来说是 modeladmin list_display:
http:// docs.djangoproject.com/en/1.2/intro/tutorial02/#customize-the-admin-change-list
如何添加一列来显示列表中每个投票的选项数量?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无需编辑模型,即可动态计算管理列,在 ModelAdmin 对象中创建一个采用第二个参数的函数,这将是常规 Poll 模型实例。与像在模型中一样编写代码相比,您可以忽略此处的 self,因为它没有您想要的内容。
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.
添加一个自定义方法(例如
pcount
),该方法返回给定Poll
实例的选择数量。然后,您可以将其添加到ModelAdmin
子类中的list_display
属性中。Add a custom method (say
pcount
) that returns the number of choices for a givenPoll
instance. You can then add this to thelist_display
attribute in yourModelAdmin
subclass.