Thursday, April 21, 2011

Admin, two links to different views ?

Hi guys, in django admin the views that show the register's just have a link to "edit", but what happen if a need an extra(S) links to another views? for example: i have view that show the list of registered People, the nick is linking to the Edit page (the normal way of Django), but i need another links that will show me the "articles" of the people and another the "comments" of the people. how ill make this with django admin? Thanks

From stackoverflow
  • (I'm assuming some field names from your models to answer)

    Make the author field from "comment" searchable:

    class CommentAdmin(admin.ModelAdmin):
       search_fields = ("=author",)
    

    Use list_display and HTML to control what's displayed on the people's list admin page:

    def comments(obj):
        return ('<a href="/admin/pathto/comments/?q=%s">comments</a>' % obj.name)
    comments.short_description = 'comments'
    comments.allow_tags = True
    
    class PeopleAdmin(admin.ModelAdmin):
        list_display = ("name", comments,)
    

    And change /admin/pathto/comments/ to whatever your comment's admin list page is.

    Basically you're going to direct your users to the comments search result page.

    Daniel Roseman : +1, this is absolutely the right way to do it, except I think it's better to keep the function as a method of the ModelAdmin subclass - in which case it will be `def comments(self, obj)`.
    Asinox : Thanks guys, let me try :)

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.