class customerDetailView(DetailView):
queryset = Customer.objects.get(name=self.name) # This line give error NameError: name 'self' is not defined
context_object_name = 'customerDetail'
template_name = "customer.html"
allow_empty = True
def __init__(self, name=None, *args):
self.name = name
gives an error NameError: name ‘self’ is not defined
Since you’re wanting to customize a
DetailView‘s queryset, the correct way to do this is override theget_queryset()function. See the documentation for DetailView which shows the method resolution order. In particular,get_queryset()is called.So your code would become this:
You cannot use
selfin the manner you are because aselfdoes not exist at class declaration, only when an instance of the class is created.