简体   繁体   English

django 站点并在管理编辑器中显示 id

[英]django sites and showing id in admin editor

I'm using the Site module in django.我在 django 中使用Site模块。 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 .但是,我真的很想查看主键 ID,因为我通过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.现在我可以通过编辑./venv/lib/python3.7/site-packages/django/contrib/sites/admin.py来做到这一点,但这在很多方面都是糟糕的形式。 I'd just add "id", thus:我只是添加“id”,因此:

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:我在我的一个models.py文件中执行了以下操作,这有助于 shell 但未显示在管理员中:

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:在您的 admin.py 中,添加以下代码:

from django.contrib.sites.models import Site

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


admin.site.unregister(Site)
admin.site.register(Site, SiteAdmin)

Since Site is already registered in admin, you will have to unregister is first, then register it again.由于Site已在管理员中注册,因此您必须先unregister ,然后再重新注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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