Django Admin 不显示条目
如果我为一个特定模型创建一个新条目,它不会显示在 django 管理中。
代理模式造成了麻烦。
# catalog.models
class Content(models.Model):
class Meta:
abstract = True
BUNDESLAND_CHOICES = (
('bw', 'Baden-Württemberg'),
('by', 'Bayern'),
('be', 'Berlin'),
('bb', 'Brandenburg'),
('hb', 'Bremen'),
('hh', 'Hamburg'),
('he', 'Hessen'),
('mv', 'Mecklenburg-Vorpommern'),
('ni', 'Niedersachsen'),
('nw', 'Nordrhein-Westfalen'),
('rp', 'Rheinland-Pfalz'),
('sl', 'Saarland'),
('sn', 'Sachsen'),
('st', 'Sachsen-Anhalt'),
('sh', 'Schleswig-Holstein'),
('th', 'Thüringen'),
)
name = models.CharField(max_length=255, verbose_name='Agentur')
address = models.CharField(max_length=255, verbose_name='Straße')
state = models.CharField(max_length=2, choices=BUNDESLAND_CHOICES, verbose_name='Bundesland')
city = models.CharField(max_length=255, verbose_name='Stadt')
zip = models.CharField(max_length=10, verbose_name='PLZ')
phone = models.CharField(max_length=40, blank=True, verbose_name='Telefonnr.')
fax = models.CharField(max_length=40, blank=True, verbose_name='Fax')
email = models.EmailField(verbose_name='E-Mail', help_text='Offizielle E-Mail')
url = models.URLField(verbose_name='URL')
owner = models.CharField(max_length=255, verbose_name='Besitzer')
description = models.TextField(verbose_name='Beschreibung')
category = models.ManyToManyField(Category, verbose_name='Kategorie')
user = models.ForeignKey(User, verbose_name='Benutzer', null=True, blank=True)
slug = models.SlugField(max_length=80, blank=True)
identity = models.CharField(max_length=64, unique=True, blank=True)
identity_used = models.BooleanField(default=False)
show = models.BooleanField(default=False, verbose_name='Anzeigen')
tp = models.DateTimeField(auto_now_add=True)
# agency.models
class AgencyActiveManager(models.Manager):
def get_query_set(self):
return super(AgencyActiveManager,self).get_query_set().filter(show=True)
class Agency(Content):
clients = models.TextField(verbose_name='Klienten')
active = AgencyActiveManager()
objects = models.Manager()
def __unicode__(self):
return self.name
def save(self, **kwargs):
if not self.identity:
self.identity = hashlib.sha256('%s:%s' %(get_word(),datetime.datetime.now())).hexdigest()
if not self.slug:
self.slug = slugify(self.name, instance=self)
super(Agency, self).save(**kwargs)
# agency.admin
from django.contrib import admin
from agency.models import Agency
admin.site.register(Agency)
我在管理员中创建了一个新的代理条目并将其保存。 通过 python shell 查询显示保存有效
In [15]: Agency.objects.all()
Out[15]: [<Agency: Argentur>]
管理页面显示: '0 Agencys'
如果我尝试手动调用 url /admin/agency/agency/1/ 我收到 404 消息,表示没有主键为 1 的代理对象
找不到页面 (404) 请求方式:GET 请求网址: http://localhost:8000/admin/agency/agency/1/
Das Agency-Objekt mit dem Primärschlüssel u'1' ist nicht vorhanden。
您看到此错误是因为您的 Django 设置文件中包含 DEBUG = True。将其更改为 False,Django 将显示标准的 404 页面。
但是 pythons shell 说:
In [16]: Agency.objects.all()[0].pk
Out[16]: 1
我是否遗漏了一些非常明显的东西?
我的猜测是它与抽象模型有关,但我不知道是什么。
If I create a new entry for one particular model it doesn't show up in the django admin.
The Agency Model is causing the trouble.
# catalog.models
class Content(models.Model):
class Meta:
abstract = True
BUNDESLAND_CHOICES = (
('bw', 'Baden-Württemberg'),
('by', 'Bayern'),
('be', 'Berlin'),
('bb', 'Brandenburg'),
('hb', 'Bremen'),
('hh', 'Hamburg'),
('he', 'Hessen'),
('mv', 'Mecklenburg-Vorpommern'),
('ni', 'Niedersachsen'),
('nw', 'Nordrhein-Westfalen'),
('rp', 'Rheinland-Pfalz'),
('sl', 'Saarland'),
('sn', 'Sachsen'),
('st', 'Sachsen-Anhalt'),
('sh', 'Schleswig-Holstein'),
('th', 'Thüringen'),
)
name = models.CharField(max_length=255, verbose_name='Agentur')
address = models.CharField(max_length=255, verbose_name='Straße')
state = models.CharField(max_length=2, choices=BUNDESLAND_CHOICES, verbose_name='Bundesland')
city = models.CharField(max_length=255, verbose_name='Stadt')
zip = models.CharField(max_length=10, verbose_name='PLZ')
phone = models.CharField(max_length=40, blank=True, verbose_name='Telefonnr.')
fax = models.CharField(max_length=40, blank=True, verbose_name='Fax')
email = models.EmailField(verbose_name='E-Mail', help_text='Offizielle E-Mail')
url = models.URLField(verbose_name='URL')
owner = models.CharField(max_length=255, verbose_name='Besitzer')
description = models.TextField(verbose_name='Beschreibung')
category = models.ManyToManyField(Category, verbose_name='Kategorie')
user = models.ForeignKey(User, verbose_name='Benutzer', null=True, blank=True)
slug = models.SlugField(max_length=80, blank=True)
identity = models.CharField(max_length=64, unique=True, blank=True)
identity_used = models.BooleanField(default=False)
show = models.BooleanField(default=False, verbose_name='Anzeigen')
tp = models.DateTimeField(auto_now_add=True)
# agency.models
class AgencyActiveManager(models.Manager):
def get_query_set(self):
return super(AgencyActiveManager,self).get_query_set().filter(show=True)
class Agency(Content):
clients = models.TextField(verbose_name='Klienten')
active = AgencyActiveManager()
objects = models.Manager()
def __unicode__(self):
return self.name
def save(self, **kwargs):
if not self.identity:
self.identity = hashlib.sha256('%s:%s' %(get_word(),datetime.datetime.now())).hexdigest()
if not self.slug:
self.slug = slugify(self.name, instance=self)
super(Agency, self).save(**kwargs)
# agency.admin
from django.contrib import admin
from agency.models import Agency
admin.site.register(Agency)
I created a new Agency entry in the admin and saved it.
Querying via the python shell shows that the save worked
In [15]: Agency.objects.all()
Out[15]: [<Agency: Argentur>]
The admin page says:
'0 agencys'
If I try manually calling the url /admin/agency/agency/1/ I get a 404 saying that there is no agency object with a primarykey 1
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/admin/agency/agency/1/Das agency-Objekt mit dem Primärschlüssel u'1' ist nicht vorhanden.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
But pythons shell says:
In [16]: Agency.objects.all()[0].pk
Out[16]: 1
Am I missing something extremely obvious?
My guess would be that it has something todo with the abstract model but I can't figure out what.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模型类定义中列出的第一个管理器是用于管理站点和许多其他操作的管理器。
Django 中存在许多与使用不返回所有实例作为默认管理器的管理器相关的错误。恕我直言,您最好使用标准管理器作为默认值,然后添加更多限制性的管理器。
事实上,我不再编写使用多个管理器的模型。我会将
其编写为
Agency.objects
并使用,因此活动对象的根查询集是Agency.objects.active()
而不是Agency.active.all ()
。这意味着.objects.
将始终具有相同的众所周知的行为。在代码中也更容易发现和理解。The first manager listed in the model class definition is the one that is used for the admin site and a number of other operations.
There have been a number of bugs in Django related to using a manager that does not return all instances as the default manager. IMHO, you are best to use a standard manager as the default, and add any more restrictive ones afterwards.
In fact, I no longer write models that use more than one manager. I would write
and use this as
Agency.objects
, so a root QuerySet for active objects isAgency.objects.active()
rather thanAgency.active.all()
. This means.objects.
will always have the same well-known behaviour. It's also easier to spot and understand in code.