BertrandBordage / django-viewsets

Avoid boring views and urls.
BSD 3-Clause "New" or "Revised" License
61 stars 9 forks source link

Create a way to individually customize the form for CreateView and UpdateView #1

Open luzfcb opened 10 years ago

luzfcb commented 10 years ago

@BertrandBordage

Really cool your library. Is there any way to make it most customizable?

Individually customize the form for CreateView and UpdateView, individually customize success_url for CreateView, UpdateView, DeleteView or use django-table2 SingleTableView instead of ListView?

BertrandBordage commented 10 years ago

Thanks :)

Yes, you can achieve this in two different ways:

You can subclass a view and then register it to the viewset (I highly recommend this implementation):

from django.views.generic import UpdateView
from viewsets import ModelViewSet

class YourUpdateView(UpdateView):
    success_url = '/your/custom/url'

class YourViewSet(ModelViewSet):
    def __init__(self, *args, **kwargs):
        self.views['update_view']['view'] = YourUpdateView
        super(YourViewSet, self).__init__(*args, **kwargs)

Or by overriding keys of the views attribute in your __init__ method. The 'kwargs' key can be used to override some view attributes or methods.

from viewsets import ModelViewSet

class YourViewSet(ModelViewSet):
    def __init__(self, *args, **kwargs):
        self.views['update_view']['kwargs'] = {'success_url': '/your/custom/url'}
        super(YourViewSet, self).__init__(*args, **kwargs)

(I know, this is complex and implies a lot of code noise. That whole dict idea should be changed to an object-oriented approach.)