Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create custom methods in django class base views

I want to use generic class base views using django 1.9 What i am trying to understand that

from django.views.generic import CreateView
from braces.views import LoginRequiredMixin
from .models import Invoice

class InvoiceCreateView(LoginRequiredMixin,CreateView):
    model = Invoice

    def generate_invoice(self):
        ...
        return invoice

now i want to bind this custom method to url. How can i achive this? I know using function base view its simple but i want to do this using class base views.

Help will be appreciated.

like image 692
Wagh Avatar asked Feb 15 '16 10:02

Wagh


People also ask

How do I create a class-based view in Django?

Asynchronous class-based viewsimport asyncio from django. http import HttpResponse from django. views import View class AsyncView(View): async def get(self, request, *args, **kwargs): # Perform io-blocking view logic using await, sleep for example. await asyncio.

Which is better class-based view or function based view Django?

Class based views are excellent if you want to implement a fully functional CRUD operations in your Django application, and the same will take little time & effort to implement using function based views.

How do you add decorators to class-based view?

To decorate every instance of a class-based view, you need to decorate the class definition itself. To do this you apply the decorator to the dispatch() method of the class. The decorators will process a request in the order they are passed to the decorator.

What is the difference between class-based views and function based views?

Class-based views are the alternatives of function-based views. It is implemented in the projects as Python objects instead of functions. Class-based views don't replace function-based views, but they do have certain advantages over function-based views.


1 Answers

Yes, this is the main issue to grasp in CBV: when things run, what is the order of execution (see http://lukeplant.me.uk/blog/posts/djangos-cbvs-were-a-mistake/).

In a nutshell, every class based view has an order of running things, each with it's own method.

CBV have a dedicated method for each step of execution.

You would call your custom method from the method that runs the step where you want to call your custom method from. If you, say, want to run your method after the view found that the form is valid, you do something like this:

Class InvoiceCreateView(LoginRequiredMixin,CreateView):
    model = Invoice

    def generate_invoice(self):
        ... do something with self.object
        return invoice

    def form_valid(self,form):

        self.object = form.save()
        self.generate_invoice()
        return super(InvoiceCreateView,self).form_valid(form)

So you have to decide where your custom method should run, and define your own method on top of the view generic method for this step.

How do you know what generic method is used for each step of executing the view? That the method the view calls when it gets the initial data for the form is def get_initial? From the django docs, and https://ccbv.co.uk/. It looks complex, but you actually have to write very few methods, just where you need to add your own behaviour.

like image 53
Aviah Laor Avatar answered Sep 19 '22 14:09

Aviah Laor