Reputation: 5477
I have an Entry class for a blog which has two fields, live and draft, which reference a sustom models.Manager. This works great across my app; however the admin is somehow referencing my LiveEntryManager.
Here are the two managers:
class LiveEntryManager(models.Manager):
def get_query_set(self):
return super(LiveEntryManager, self).get_queryset().filter(status=self.model.LIVE_STATUS)
class DraftEntryManager(models.Manager):
def get_query_set(self):
return super(DraftEntryManager, self).get_queryset().filter(status=self.model.DRAFT_STATUS)
Here is my class:
class Entry(models.Model):
LIVE_STATUS = 1
DRAFT_STATUS = 2
HIDDEN_STATUS = 3
STATUS_CHOICES = (
(LIVE_STATUS, 'Live'),
(DRAFT_STATUS, 'Draft'),
(HIDDEN_STATUS, 'Hidden'),
)
title = models.CharField(max_length=250)
excerpt = models.TextField(blank=True, help_text='Excerpt Text')
body = models.TextField(help_text='Body Text')
pub_date = models.DateField()
slug = models.SlugField(unique_for_date='pub_date')
pub_date = models.DateTimeField(default=datetime.datetime.now)
author = models.ForeignKey(User)
enable_comments = models.BooleanField(default=True)
status = models.IntegerField(choices=STATUS_CHOICES, default="LIVE_STATUS")
categories = models.ManyToManyField(Category)
excerpt_html = models.TextField(editable=False, blank=True)
body_html = models.TextField(editable=False, blank=True)
live = LiveEntryManager()
draft = DraftEntryManager()
objects = models.Manager()
class Meta:
verbose_name_plural = "Entries"
ordering = ['-pub_date']
def __unicode__(self):
return self.title
I'm using this in my urls below:
url(r'^$',
ArchiveIndexView.as_view(queryset=Entry.live.all(),
date_field='pub_date'),
name='coltrane_entry_archive_index'),
url(r'^draft/$',
ArchiveIndexView.as_view(queryset=Entry.draft.all(),
date_field='pub_date'),
name='coltrane_entry_archive_index'),
My issue is the admin page returns only results based on the LiveEntryManager. Once I change them from live to draft they are removed from the Entries admin display. I'm not sure how the admin is using that manager to filter it's results.
My logic in the admin page is pretty simple:
from models import Entry
class EntryAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ['title']}
admin.site.register(Entry, EntryAdmin)
Upvotes: 0
Views: 141
Reputation: 11760
Move objects = models.Manager()
up in your Entry model so that it's the first manager. The first manager in a model is the default manager (doc) and that's what the Admin is using (code).
Upvotes: 2
Reputation: 1738
Looks like both urls have the same name. Maybe that's your problem?
Upvotes: 0