在Django中按类别渲染对象

在Django中按类别渲染对象,django,Django,我创建了一个模型,在这个模型中,管理员通过新帖子或独占帖子对帖子进行分类。现在我想查看帖子,这些都是新的。代码如下: class Post(models.Model): class Category(models.TextChoices): NEW = 'NP', ('New Post') EXCLUSIVE = 'EP', ('Exclusive Post') post_category = models.CharField(

我创建了一个模型,在这个模型中,管理员通过新帖子或独占帖子对帖子进行分类。现在我想查看帖子,这些都是新的。代码如下:

class Post(models.Model):
    class Category(models.TextChoices):
        NEW = 'NP', ('New Post')
        EXCLUSIVE = 'EP', ('Exclusive Post')


    post_category = models.CharField(
        max_length=2,
        choices=Category.choices,
        default=Category.NEW,
    )
    title = models.CharField('Title of the post',max_length=50)
    content = models.TextField('Page content', max_length=500)
    posted = models.DateTimeField('last updated')
    
    def __str__(self):
        return self.title
    
    def is_new(self):
        return self.post_category in{
            self.Category.NEW,
            self.Category.EXCLUSIVE,
        }
观点:

def index(request):
    post_list = Post.objects.filter(post_category).order_by('-posted')[:2]
    context = {
        'post_list': post_list,
        'page_list': Pages.objects.all(),

    }

    return render(request, 'pages/extension.html', context)
尝试:

post\u list=post.objects.filter(post\u category=post.category.NEW)

Kowalinski它起作用了,但方式有点不同。我试过post_category=post.category.NEW。无论如何,谢谢。哦,是的,你说得对。我忘了这是一个内部类,但很好,你让它工作