所以,我认为我遗漏了一些非常明显的东西,或者错误地设置了我的模型,我试图在我的博客上创建一个过滤类别slug的视图,但我的过滤器只适用于Entry slug;请参阅下面的模型以及我的观点。任何帮助将不胜感激。谢谢。
我的模特
from django.db import models
from django.core.urlresolvers import reverse
from django_markdown.models import MarkdownField
class EntryQuerySet(models.QuerySet):
def published(self):
return self.filter(publish=True)
class Tag(models.Model):
slug = models.SlugField(max_length=200, unique=True)
def __str__(self):
return self.slug
class Category(models.Model):
slug = models.SlugField(max_length=100, blank=False)
def __str__(self):
return self.slug
class Entry(models.Model):
title = models.CharField(max_length=200)
body = MarkdownField()
slug = models.SlugField(max_length=200, unique=True)
publish = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
tags = models.ManyToManyField(Tag)
category = models.ForeignKey(Category)
objects = EntryQuerySet.as_manager()
def get_absolute_url(self):
return reverse("post_detail", kwargs={"slug": self.slug})
def __str__(self):
return self.title
class Meta:
verbose_name = "Blog Entry"
verbose_name_plural = "Blog Entries"
ordering = ["-created"]
我的观点
from django.views import generic
from . import models
class BlogIndex(generic.ListView):
queryset = models.Entry.objects.published()
template_name = "blog/post_list.html"
class BlogDetail(generic.DetailView):
model = models.Entry
template_name = "blog/post_detail.html"
# Marketing Pages
class GlamisView(generic.ListView):
queryset = models.Entry.objects.filter(slug="glamis")
template_name = "blog/glamis_list.html"
答案 0 :(得分:1)
如果我理解你,你可以使用查找表达式。您的查询将如下所示:
queryset = models.Entry.objects.filter(category__slug="glamis")