`filter(category_slug=category)` 是什么意思?
What does `filter(category_slug=category)` mean?
我有以下 models.py
设置,
class PostList(RoutablePageMixin, Page):
template = "Post_List.html"
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel("intro")
]
subpage_types = [
"PostDetail",
]
parent_page_type = [
"HomePage",
]
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context['posts'] = self.posts
context['post_list'] = self
return context
def get_posts(self):
return PostList.objects.descendant_of(self).live()
@route(r'^tag/(?P<tag>[-\w]+)/$')
def post_by_tag(self, request, tag, *args, **kwargs):
self.search_type = 'tag'
self.search_term = tag
self.posts = self.get_posts().filter(tag__slug=tag)
return Page.serve(self, request, *args, **kwargs)
@route(r'^category/(?P<category>[-\w]+)/$')
def post_by_category(self, request, category, *args, **kwargs):
self.search_type = 'category'
self.search_term = category
self.posts = self.get_posts().filter(category__slug=category)
return Page.serve(self, request, *args, **kwargs)
@route(r'^$')
def post_list(self, request, *args, **kwargs):
self.posts = self.get_posts()
return Page.serve(self, request, *args, **kwargs)
问:我不明白filter(category__slug=category)
中的category__slug是什么意思?
根据field lookup in Django official doc,我从未见过这样的过滤器,只看到xxx__gt或xxx__exact等
I do not understand what does category__slug mean in filter(category__slug=category)
.
这意味着我们正在过滤 post 个对象的 category
的 slug
字段。 __slug
因此 不是 查找,它是 Category
模型的一个字段。因此,该模型将如下所示:
class Category(models.Model):
<strong>slug</strong> = models.SlugField()
# …
class Post(models.Model):
<strong>category</strong> = models.ForeignKey(Category, on_delete=models.CASCADE)
# …
因此我们将进行如下查询:
SELECT post.*
来自 post
INNER JOIN 类别开启 post.category_id = category_id
WHERE category.slug = <em>类别变量值</em>
我有以下 models.py
设置,
class PostList(RoutablePageMixin, Page):
template = "Post_List.html"
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel("intro")
]
subpage_types = [
"PostDetail",
]
parent_page_type = [
"HomePage",
]
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context['posts'] = self.posts
context['post_list'] = self
return context
def get_posts(self):
return PostList.objects.descendant_of(self).live()
@route(r'^tag/(?P<tag>[-\w]+)/$')
def post_by_tag(self, request, tag, *args, **kwargs):
self.search_type = 'tag'
self.search_term = tag
self.posts = self.get_posts().filter(tag__slug=tag)
return Page.serve(self, request, *args, **kwargs)
@route(r'^category/(?P<category>[-\w]+)/$')
def post_by_category(self, request, category, *args, **kwargs):
self.search_type = 'category'
self.search_term = category
self.posts = self.get_posts().filter(category__slug=category)
return Page.serve(self, request, *args, **kwargs)
@route(r'^$')
def post_list(self, request, *args, **kwargs):
self.posts = self.get_posts()
return Page.serve(self, request, *args, **kwargs)
问:我不明白filter(category__slug=category)
中的category__slug是什么意思?
根据field lookup in Django official doc,我从未见过这样的过滤器,只看到xxx__gt或xxx__exact等
I do not understand what does category__slug mean in
filter(category__slug=category)
.
这意味着我们正在过滤 post 个对象的 category
的 slug
字段。 __slug
因此 不是 查找,它是 Category
模型的一个字段。因此,该模型将如下所示:
class Category(models.Model):
<strong>slug</strong> = models.SlugField()
# …
class Post(models.Model):
<strong>category</strong> = models.ForeignKey(Category, on_delete=models.CASCADE)
# …
因此我们将进行如下查询:
SELECT post.*
来自 post
INNER JOIN 类别开启 post.category_id = category_id
WHERE category.slug = <em>类别变量值</em>