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 python-3.x, 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.