【发布时间】:2013-05-29 15:42:27
【问题描述】:
所以背景细节,Post是一个模型,我基本上是在尝试创建一个博客,与video中显示的博客相同。
代码如下:
from django.views.generic import ListView, DetailView
from models import Post
class PublishedPostsMixin(object):
def get_queryset(self):
queryset = super(PublishedPostsMixin, self).get_queryset()
return queryset.filter(published=True)
class PostListView(PublishedPostsMixin, ListView):
# PostListView takes default template name as `post_list.html`,
# as list was the name it was assigned.
model = Post
template_name = 'blog/post_list.html'
class PostDetailView(PublishedPostsMixin, DetailView):
model = Post
template_name = 'blog/post_detail.html'
所以,如果你可以看到,PublishedPostsMixin 是从对象继承的,那么super() 是如何工作的。如果您能理解发生了什么,请您一步一步解释,我有点困惑。
【问题讨论】:
-
mixin 不应该单独使用。它仅在应用于定义
get_queryset方法的类时才有效。 -
@AdriánLópez:谢谢,我知道,但它是如何工作的。