Lately I was learning Django and Django rest framework from the official docs and i stumbled upon a method defined inside the GenericAPIView named get_queryset. Till now everything is fine
but i was lost when he override this latter
the code is as follow:
def get_queryset(self):
user = self.request.user
return user.accounts.all()
My question is simple, the request is not defined in the method’s argument
so where did it come from?
>Solution :
It’s a member of the View parent class. The class that this method is inside of will inherit from View or a child class of View. Something like:
class YourView(View):
. . .
By inheriting from View, it inherits behaviors as well, like the setup function that’s called that populates that attribute.
You may want to review classes and inheritance before continuing with Django. Django makes quite extensive use of inheritance and some advanced class features like metaclasses.