[Django]-How to add the auto primary key into django's admin list_display

5👍

The automatically created primary key field works just like any other field, except that you don’t have to specify it. Unless you’ve changed the name, this should be as simple as:

list_display = ['id', ...]

I haven’t checked on whether or not the pk alias works everywhere that the admin expects field names.
UPDATE: the pk alias is accepted instead of id

6👍

Yes, in the list_display field, you can add the pk field.

To make it linkable, you can use the list_display_links field

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('pk', 'name', 'email', ...) #You can use 'pk' or 'id'
    list_display_links = ('pk', )   #the same here - 'pk' or 'id'

admin.register(MyModel, MyModelAdmin)

Leave a comment