Custom Search

Thursday, August 21, 2014

Django ModelAdmin add new custom column to changelist table

* Add a new item "get_userid" to list_display.
* Create a new method "get_userid" return the value you want to display in the new column in the changelist table

class StatusAdmin(admin.ModelAdmin):
    ##SM:added
    form = StatusAdminForm
    list_display = ('year', 'book', 'user', 'get_userid')


    def get_userid(self, obj):
        ##Add custom column to the table
        #http://stackoverflow.com/questions/163823/can-list-display-in-a-django-modeladmin-display-attributes-of-foreignkey-field
        return obj.user.id
    get_userid.short_description = 'User ID'



No comments:

Post a Comment