如何在list_display中显示内联元素?
我有以下问题:
我有两个模型:文章和评论,在评论中,我有parent = models.ForeignKey(Article)。我已将其设置为将 Comments 内联到 ArticleAdmin(admin.ModelAdmin) 和 CommentInline(admin.StackedInline)。我想要的是,对于文章列表视图(在 list_display 中选择的元素),我想显示最新评论的片段,以便用户不必单击每个单独的评论来查看更改。现在我知道我可以在 list_display 中指定一个函数,但我不确定如何在函数中轻松完成我希望做的事情。
有人对如何实现这一目标有任何建议吗?
非常感谢您的帮助!
I have the following problem:
I have two models: Article and Comment, in Comments, i have parent = models.ForeignKey(Article). I have it set up so that Comments is inline to ArticleAdmin(admin.ModelAdmin), and CommentInline(admin.StackedInline). What i would like is that for Article list view (elements chosen in list_display), I would like to display snippets of latest comments so that the user does not have to click into each individual comments to see the changes. Now i know that i can specify a function in list_display, but i'm not sure how to do what i wish to do easily in the functions.
anyone have any suggestion on how to go about accomplishing this?
Thank you very much for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所说,定义一个函数是可行的方法 - ModelAdmin 类上的自定义方法,该方法将对象作为参数并返回最新评论的字符串表示形式:
这将获取每篇文章的最后三个评论,按'date' 字段,并显示每个字段的
comment
字段,由 HTML
标记分隔以在每行显示一个。As you say, defining a function is the way to go - a custom method on the ModelAdmin class which takes the object as a parameter and returns a string representation of the latest comments:
This takes the last three comments on each article, sorted by the 'date' field, and displays the
comment
field of each one, separated by an HTML<br>
tag to show on one each line.