Add foreign key name to admin's list_display?

1,872 views
Skip to first unread message

Ty

unread,
Oct 17, 2008, 3:21:43 PM10/17/08
to Django users
# admin.py:
from django.contrib import admin
from clydefrog.blog.models import Post, Comment

class PostAdmin(admin.ModelAdmin):
list_display = ('name', 'slug', 'date', 'is_published',)
list_filter = ('is_published',)
ordering = ('is_published', '-date', 'name',)
search_fields = ['name', 'raw_body',]

class CommentAdmin(admin.ModelAdmin):
list_display = ('name', 'email', 'website', 'date', 'approved',
'is_admin',)
list_filter = ('approved', 'is_admin',)
ordering = ('-date','name',)
search_fields = ['raw_body','name','email','website',]

admin.site.register(Post, PostAdmin)
admin.site.register(Comment, CommentAdmin)

------------------------------------------------------------------

Hey everyone. This is working perfectly for me, however I'd like to
display what post each comment belongs to in the comment
administration list.

I've tried something like this but I can't get anything working:
list_display = ('Post.name', 'name', 'email', 'website', 'date',
'approved', 'is_admin',)

Is this currently possible?

Karen Tracey

unread,
Oct 17, 2008, 4:16:10 PM10/17/08
to django...@googlegroups.com

Certainly.  Here's the doc on what you can put in list_display:

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#list-display

If the __unicode__ method of the Post model displays its name, then you simply add the name of the ForeignKey field for Post in Comment and you'll get what you were looking for by trying 'Post.name'.  If Post's __unicode__ method isn't what you want to see, write a method on the CommentAdmin model that returns what you want to see and include it in list_display.

Karen

Ty

unread,
Oct 17, 2008, 4:31:06 PM10/17/08
to Django users
Great! Changing the list_display to 'post' and changing the return
value of Post's __unicode__ worked perfectly. Thanks for your help!

On Oct 17, 4:16 pm, "Karen Tracey" <kmtra...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages