I am working on a django blog project.
this is my models.py code:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class PublishManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status=Post.Status.PUBLISHED)
class Post(models.Model):
class Status(models.TextChoices):
DRAFT = 'DF', 'Draft'
PUBLISHED = 'PB', 'Published'
user = models.ForeignKey(User,on_delete=models.CASCADE,related_name="blog_posts")
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250)
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=2,choices=Status.choices,default=Status.DRAFT)
objects = models.Manager()
published = PublishManager()
class Meta:
ordering = ['-publish']
indexes = [
models.Index(fields=['-publish'])
]
def __str__(self):
return self.title
when i put “published = PublishManager()“ on top of “objects = models.Manager()” like this:
published = PublishManager()
objects = models.Manager()
i found that my admin site didnt show the entry with draft status.
if i create a Post object with status equal Published,then it will be show in my admin site.if i create an object with status Draft,it won't show in admin site.
i don't know why,could anyone can help me?
i guess, maybe the default manager is the first manager.the admin site use the first manager to show all entry.