Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'ProductList' object has no attribute 'object_list'

In my ProductList classs, when I try to call get_context_data in another method, I get an error 'ProductList' object has no attribute 'object_list'

def get_context_data(self, **kwargs):
        c = super(ProductList, self).get_context_data(**kwargs)
        c['category'] = self.category
        c['category_menu'] = self.get_category_menu()
        c['filters'] = self.filters
        c['expanded_filters'] = self.get_expanded_filters()
        c['active_filters'] = self.get_active_filters()
        c['category_list'] = self.category.get_children().filter(in_lists=True)
        c['colors_list'] = self.get_colors_list(c['object_list'])
        return c

def get_queryset(self):

    data = self.get_context_data()

What's causing this error?

How can I get object_list in my second method?

like image 217
Viktor Avatar asked Jun 07 '16 09:06

Viktor


People also ask

Why list object has no attribute “values”?

The "AttributeError: 'list' object has no attribute 'values'" occurs when we try to call the values () method on a list instead of a dictionary. You can view all the attributes an object has by using the dir () function.

How do I fix attributeerror 'list' object has no attribute 'replace'?

The Python "AttributeError: 'list' object has no attribute 'replace'" occurs when we call the replace () method on a list instead of a string. To solve the error, call replace () on a string, e.g. by accessing the list at a specific index or by iterating over the list.

What is “object has no attribute Python error”?

Attributes are functions or properties associated with an object of a class. Everything in Python is an object, and all these objects have a class with some attributes. We can access such properties using the . operator. This tutorial will discuss the object has no attribute python error in Python. This error belongs to the AttributeError type.

How do I get the default value of a list attribute?

The list doesn’t have an attribute size, so it returns False. If we want an attribute to return a default value, we can use the setattr () function. This function is used to create any missing attribute with the given value. See this example.


Video Answer


2 Answers

You might be getting the error on following line in get_context_data() of the super class:

queryset = kwargs.pop('object_list', self.object_list)

The get method of BaseListView sets the object_list on the view by calling the get_queryset method:

self.object_list = self.get_queryset()

But, in your case, you are calling get_context_data() in get_queryset method itself and at that time object_list is not set on the view.

like image 159
AKS Avatar answered Oct 19 '22 20:10

AKS


Sorry for such a blurred question. It's just my first try of Django. I read some documentation and realized that I can actually get a list of objects I need with the filter():

data = self.model.objects.filter(categories__in=self.get_category_menu())
like image 21
Viktor Avatar answered Oct 19 '22 19:10

Viktor