built in methods for Django CBV's

Viewed 121

I am learning Django out of pure passion and I seem to have trouble understanding some core principles of Django that really give me headackes and i tried searching online and reading the docs but i don't seem to fully understand. I'll just simply shoot the questions and try to be clear . I apologise for anything stupid I say . I am just trying to get my knowledge straight.

1. What is a "request"?

OK, so I am thinking of a GET request for a webpage but online i see python code like self.request.user.is_superuser and i am thinking of an User object fetched and displayed to the template to which i can apply User methods. It is clearly more to this request than i already know.

2. CBV's built in methods. The get methods. How do they execute? In what order.

I noticed programmers override these methods sometime . If i redefine more than one method in a CBV i start getting weird behaviour.

For example if i declare and redefine 2 methods in a Detail View get_object() and get_queryset() which of them will be executed first after calling the view? Being a method it should be called somehow in order to execute but i don't know the order how these methods are called for a CBV if there is more than one. Probably i should only define one of them, not both.

3. Queryset. Is it a list?

I have an ideea what querysets are and that you can apply filters to them but what do they return? Do they return objects of the model i am querying?.

OK so, if i have a DetailView CBV and override the get_queryset() method will this return on object and pass it to my template?. Here I am using a filter but what if am not using a filter. What will the CBV return? Will it return the entire set of objects?

class UserDetailView(LoginRequiredMixin,DetailView):
    context_object_name='user_detail'
    model=models.User
    template_name='basicapp/user_detail.html' 
    def get_queryset(self, *args, **kwargs):
        qs = super().get_queryset(*args, **kwargs)
        if not self.request.user.is_superuser:
            qs = qs.filter(pk=self.request.user.pk)
        return qs

4. Can you kindly recommend a Udemy course, video course or book where querysets, CBV methods and ORM are explained clearly?

Preferably video cause it makes so much difference when i see the code working

The thing is I understood how the MVT mechanism works but these class built in methods and overriding them gives me a lot of problems . I never encountered them before and when i start using them I feel like as if i am walking in the dark .

Sorry for the long post. Thanks

1 Answers
  1. What is a request?

It is a HttpRequest object [Django-doc]. This is an object that contains data about the request the client made to the server. For example this contains an attribute .method [Django-doc] that contains a string like 'GET', 'POST', 'PATCH', etc. that specifies the request method used.

but online i see python code like self.request.user.is_superuser and I am thinking of an User object fetched and displayed to the template to which I can apply User methods.

One can install middleware [Django-doc], this is tooling that pre-processes the request, or post-processes the response. Normally the AuthenticationMiddleware [Django-doc] is added in the settings.py file by default. This will add an extra attribute .user to the request that lazily loads the user that has been logged in. If you would remove this middleware, the .user attribute will no longer exist.

  1. CBV's built in methods. The get methods. How do they execute? In what order.

That depends on the specific class-based view. The documentation however specifies how most methods are performed. For example for a ListView [Django-doc], the documentation mentions the method flowchart:

Method Flowchart

  1. setup()
  2. dispatch()
  3. http_method_not_allowed()
  4. get_template_names()
  5. get_queryset()
  6. get_context_object_name()
  7. get_context_data()
  8. get()
  9. render_to_response()

It also links to the methods that explain what these are doing.

In essence, each class-based view can have methods like get, post, put, patch, etc. Based on the method of the request, the request is dispatched to the method with the same name.

  1. Queryset. Is it a list?

No. A QuerySet is an object that more or less represents a query that you can perform on the database. QuerySets are lazy. That means that as long as you do not iterate over them or for example call len(…) over these, they will not make that query. If you iterate, etc. over these, they will make a query to the database, and then you iterate over the result of the query. The results are normally wrapped in model objects (unless you use functions like values(…) [Django-doc] or values_list(…) [Django-doc].

  1. Can you kindly recommend a Udemy course, video course or book where querysets, CBV methods and ORM are explained clearly?

It might help to start with the Django tutorials. These go step-by-step over the architecture of Django. The documentation on the QuerySet API explains how you can make querysets. The page on Query expressions gives examples on how to make more advanced queries. Finally the Django documentation also has a page on class-based views.

Related