Django modeladmin list_display

发布于 2024-09-16 18:00:41 字数 325 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2024-09-23 18:00:41

您无需编辑模型,即可动态计算管理列,在 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()

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()
﹉夏雨初晴づ 2024-09-23 18:00:41

添加一个自定义方法(例如 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', )

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', )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文