Sunday, 15 May 2011

python - Best practices for using multiple generic views and a form within one class based view in Django 1.7 -



python - Best practices for using multiple generic views and a form within one class based view in Django 1.7 -

i'm trying figure out how utilize multiple class based generic views forms in django 1.7. familiar function based views have been trying class based views.

the way understand class based view is python class contains methods, among others, post, get, delete, , other http request methods. these methods called when 1 of request methods recieved. illustration if http received get() method within class based view executed.

now on django docs here give block of code illustration views.py file

from django.views.generic.edit import createview, updateview, deleteview django.core.urlresolvers import reverse_lazy myapp.models import author class authorcreate(createview): model = author fields = ['name'] class authorupdate(updateview): model = author fields = ['name'] class authordelete(deleteview): model = author success_url = reverse_lazy('author-list')

i bit confused though because think improve instead:

from django.views.generic.edit import createview, updateview, deleteview django.core.urlresolvers import reverse_lazy myapp.models import author class author(createview, deleteview): model = author fields = ['name'] success_url = reverse_lazy('author-list') #i assume have separate because both implement post method class authorupdate(updateview): model = author fields = ['name']

because fewer lines of code , have single class based view perform 3 of these operations. have been able find online has seemed mimic docs , create multiple class based views instead of using multiple generics in 1 class based view.

my question is, there reason code written way? , best practices using multiple generics within single class based view? or able utilize multiple in way? allowed write 3 views did above?

i'm bit confused question. 3 separate views, accessed on different urls, , presumably using different templates. there's nil gain putting them single class. on contrary, create things much more complicated: how determine if post update or delete?

you mention rest @ end. if looking kind of rest api, need django rest framework, provides generic rest-based views.

python django multiple-inheritance django-class-based-views django-generic-views

No comments:

Post a Comment