Reputation: 45
I follow Practical Django Project to write a blog. But the change list only displays entries with status LIVE. Although other entries with status DRAFT and HIDDEN are stored in database, they are not shown in change list . Anyone can help me? Thank you.
Flowing is model.py
class LiveManager(models.Manager):
def get_query_set(self):
return super(LiveManager,self).get_query_set().filter(status=self.model.LIVE_STATUS)
class Post(models.Model):
LIVE_STATUS=1
DRAFT_STATUS=2
HIDDEN_STATUS=3
STATUS_CHOICES=(
(LIVE_STATUS,'Live'),
(DRAFT_STATUS,'Draft'),
(HIDDEN_STATUS,'Hidden')
)
status=models.IntegerField(choices=STATUS_CHOICES)
category=models.ManyToManyField(Category)
title=models.CharField(max_length=100)
slug=models.SlugField(max_length=100)
content_markdown=models.TextField(blank=True)
content_markup=models.TextField(blank=True)
pub_date=models.DateTimeField()
live=LiveManager()
objects=models.Manager()
def save(self):
self.content_markup=markdown(self.content_markdown,['codehilite'])
super(Post,self).save()
def __unicode__(self):
return '%s'%(self.title)
Here is admin.py
class PostAdmin(admin.ModelAdmin):
exclude=['content_markup']
prepopulated_fields={'slug':('title',)}
list_display=['title','status']
class CategoryAdmin(admin.ModelAdmin):
prepopulated_fields={'slug':('title',)}
admin.site.register(Category,CategoryAdmin)
admin.site.register(Post,PostAdmin)
Upvotes: 1
Views: 727
Reputation: 43
ModelAdmin use self.model._default_manager. So you can also override queryset method of PostAdmin:
class PostAdmin(admin.ModelAdmin):
def queryset(self, request):
"""
Returns a QuerySet of all model instances that can be edited by the
admin site. This is used by changelist_view.
"""
qs = self.model.objects.get_query_set()
ordering = self.get_ordering(request)
if ordering:
qs = qs.order_by(*ordering)
return qs
...
Or you can set _default_manager = objects in Post model.
Upvotes: 0
Reputation: 6355
According to the docs:
"...the first Manager Django encounters (in the order in which they’re defined in the model) has a special status. Django interprets the first Manager defined in a class as the “default” Manager"
So just make sure objects=models.Manager()
comes before live=LiveManager()
Upvotes: 6