开发者

Extending the Django change list view to include the object title it is related to

开发者 https://www.devze.com 2023-01-10 05:54 出处:网络
I am using the Django comments framework to allow commenting on articles in a blog. I want to display the title of the article that the comment(s) belongs to in the list view of the开发者_C百科 commen

I am using the Django comments framework to allow commenting on articles in a blog. I want to display the title of the article that the comment(s) belongs to in the list view of the开发者_C百科 comments section where the comment name, content type, object id etc is.

How do I do this? I know you can hook up actions into your admin.py list view by writing a model method but in this case I do not have a model for comments since I am using the built in one.

Thanks


Somewhere in your code you can override the Comments ModelAdmin class and extend it to do what you want. This code isn't tested, but it should give you a general enough idea about how to customize the Comment admin:

from django.contrib import admin
from django.contrib.comments.admin import CommentsAdmin

class MyCommentsAdmin(CommentsAdmin):

    # The callable that the list_display will call
    def show_object_title(self):
        return self.content_object.title

    list_display = super(MyCommentsAdmin, self).list_display
    list_display += ('show_object_title',)

admin.site.unregister(Comment)
admin.site.register(Comment, MyCommentsAdmin)


This doesn't work because the method gets two parameters and you have to use the second to get the title. This works:

def show_object_title(self, obj):
     return obj.content_object.title

Also the super()-call doesn't work here. At least not the way described above. It may be easier to just copy and edit the list_display-tuple from the Comments-Admin-Source

0

精彩评论

暂无评论...
验证码 换一张
取 消