首页 > 解决方案 > 这种方法有什么超级之处?

问题描述

class PublisherDetail(DetailView):

    model = Publisher

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # Add in a QuerySet of all the books
        context['book_list'] = Book.objects.all()
        return context

标签: djangodjango-viewsdjango-class-based-viewsdjango-generic-views

解决方案


根据基本的 Python 继承规则,super().get_context_data(...)应该是DetailView.get_context_data(),但由于它没有被定义,它是从SingleObjectMixin.


推荐阅读