When I use django generic view - UpdateView and try to update form kwargs like this:
#views.py
from .forms import SomeInheritedForm
from .models import SomeModel
from django.views.generic import UpdateView
class SomeUpdateView(UpdateView):
model = SomeModel
form_class = SomeInheritedForm
def get_form_kwargs(self, **kwargs):
kwargs = super(SomeUpdateView, self).get_form_kwargs(**kwargs)
kwargs['workspace'] = 'whatever'
return kwargs
#forms.py
from .models import SomeModel
from django import forms
class ParentForm(forms.Form):
def __init__(self, *args, **kwargs):
self.workspace = kwargs.pop('workspace', None)
super(ParentForm, self).__init__(*args, **kwargs)
class InheritedForm(forms.ModelForm, ParentForm):
class Meta:
model = SomeModel
This will give such an error:
Django Version: 1.7.2
Exception Type: TypeError
Exception Value:
__init__() got an unexpected keyword argument 'workspace'
Exception Location: /[ommited]/my_venv/local/lib/python2.7/site-packages/django/views/generic/edit.py in get_form, line 45
Python Executable: /[ommited]/my_venv/bin/python
Python Version: 2.7.3
However, if I declare __init__ in InheritedForm it will work fine. But then, with more inherited forms I would have to copy the code, which is against DRY principle.
Aucun commentaire:
Enregistrer un commentaire