Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why isn't my id showing up in django admin list?

I have a class Task(models.Model), and i didn't define id field explicitly (since it defines automatically for you). I checked in the database, it exists for the Task. Now i would like to display it in the list via list_display property in admin.ModelAdmin. I have a bunch of things in there, only id is not showing up for any of the rows i have. Everything else works fine. Anyone know anything special i have to do to get id to display?

EDIT: if i define a function as follows:

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

and i put this function in list_display, it will display id just fine for some reason.

Thanks a lot!

Jason

like image 695
FurtiveFelon Avatar asked Jun 18 '10 16:06

FurtiveFelon


People also ask

How do I get admin access to Django?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).


2 Answers

It doesn't show by default. You need to create a custom Admin class for that model and then add 'id' to the list_display value. E.g. in whatever/admin.py:

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

admin.site.register(Task, TaskAdmin)

See the docs for more details.

like image 166
Peter Rowell Avatar answered Sep 26 '22 16:09

Peter Rowell


I believe you have also used list_editable in your admin.ModelAdmin. This causes the ID to be hidden and is a known bug: http://code.djangoproject.com/ticket/12475.

The work around is to state 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.

like image 45
Daniel Sokolowski Avatar answered Sep 25 '22 16:09

Daniel Sokolowski