Why is my id not showing up in django admin list?

I have a Task class (models.Model), and I did not explicitly define the id field (since it automatically detects you). I checked in the database, it exists for the task. Now I would like to display it in the list via the list_display property in admin.ModelAdmin. I have a bunch of things, only the identifier is not displayed for any of the lines that I have. Everything else works fine. Does anyone know anything special that I need to do to display id?

EDIT: if I define a function as follows:

def ID(self, obj): return obj.id 

and I put this function in list_display, for some reason the identifier will be displayed.

Thanks a lot!

Jason

+4
source share
2 answers

By default, it is not displayed. You need to create your own admin class for this model, and then add "id" to the list_display value. For instance. anyway /admin.py:

 class TaskAdmin(admin.ModelAdmin): list_display = ['id', 'name', etc. etc] admin.site.register(Task, TaskAdmin) 

See documents for more details.

+4
source

I believe that you also used list_editable in your admin.ModelAdmin. This hides the identifier and is a known bug: http://code.djangoproject.com/ticket/12475 .

Work around is to specify the list_display_links option:

 class AdAdmin(admin.ModelAdmin): list_display = ['id', 'ad_title', 'status', 'sponsor' ... ] list_display_links = ['id', 'ad_title'] # forcing to display id of model list_editable = ['status'] ... 

Thanks and hope this helps.

+1
source

Source: https://habr.com/ru/post/1313205/


All Articles