开发者

Django Admin doesn't show entries

开发者 https://www.devze.com 2022-12-13 10:49 出处:网络
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.

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.


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

class AgencyManger(models.Manager):
    def active(self):
        return self.filter(show=True)

and use this as Agency.objects, so a root QuerySet for active objects is Agency.objects.active() rather than Agency.active.all(). This means .objects. will always have the same well-known behaviour. It's also easier to spot and understand in code.

0

精彩评论

暂无评论...
验证码 换一张
取 消