添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

class based views -TypeError: super(type, obj): obj must be an instance or subtype of type

Ask Question

I am building an app in Django, it uses class based views.

In my views.py I have this class based view that allows to inspect details of objects in my model Product :

class ProductDetailView(DetailView):
    queryset = Product.objects.all()
    template_name = "products/detail.html"
    def get_context_data(self, *args, **kwargs):
        context = super(ProductListView, self).get_context_data(*args, **kwargs)
        return context

As I try to run the server I get this traceback:

Traceback (most recent call last):
context = super(ProductListView, self).get_context_data(*args, **kwargs)
TypeError: super(type, obj): obj must be an instance or subtype of type

What is the problem?

As you derived yourself, the type should be an element of the Method Resolution Order (MRO) of self, so:

class ProductDetailView(DetailView):
    queryset = Product.objects.all()
    template_name = 'products/detail.html'
    def get_context_data(self, *args, **kwargs):
        context = super(ProductDetailView, self).get_context_data(*args, **kwargs)
        return context

however, since , you do not need to pass parameter to super(): If you are using the class where it is defined, and self as parameter, you can make use of super(), so you can rewrite this to:

class ProductDetailView(DetailView):
    queryset = Product.objects.all()
    template_name = 'products/detail.html'
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        return context

This thus makes it easy to define a code fragment that can be easily copy pasted to other views.

Furthermore here it makes no sense to override get_context_data, since you only call the super method and return its result, you can ommit the override.

pointing at super(type, obj)

This means that the second argument that you pass to super must be an instance or subtype of the first one.

If you look at your code, ProductListView is not an instance or subtype of self, that in the case is equal to ProductDetailView.

This is obviously a bad copy-paste issue. Replace

context = super(ProductListView, self).get_context_data(*args, **kwargs)
context = super(ProductDetailView, self).get_context_data(*args, **kwargs)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.