简体   繁体   中英

django sites and showing id in admin editor

I'm using the Site module in django. In the admin interface, I see domain name and display name. I'd really like to see the primary key id, as well, however, since I specify the sites by SITE_ID .

Now I could do this by editing ./venv/lib/python3.7/site-packages/django/contrib/sites/admin.py , but that's poor form in so many ways. I'd just add "id", thus:

class SiteAdmin(admin.ModelAdmin):
    list_display = ('id', 'domain', 'name')
    search_fields = ('id', 'domain', 'name')

I did the following in one of my models.py files, which helps in the shell but doesn't show up in admin:

def site_name(self):
    return '{domain} ({id})'.format(
        domain=self.domain, id=self.id)

Site.__str__ = site_name

Any suggestion how to do this (or pointer on what I'm doing wrong that I think I want it)?

In your admin.py, add this code:

from django.contrib.sites.models import Site

class SiteAdmin(admin.ModelAdmin):
    list_display = ('id', 'domain', 'name')
    search_fields = ('id', 'domain', 'name')




Since Site is already registered in admin, you will have to unregister is first, then register it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM