所以背景细节,Post 是一个模型,我基本上是在尝试创建一个博客,与本视频中显示的博客相同。
这是代码:
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()
工作的。如果您能理解发生了什么,请您逐步解释一下,我有点困惑。