Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

from where did the self.request come from?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading