1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class ModelFormView(FormView):
""" Use (?P<id>[\-0-9]*) urls.py lookups to populate form instance """
lookups = ['id', ]
def get_form_kwargs(self):
""" Override to add instance """
self.get_instance()
kwargs = {'initial': self.get_initial()}
if self.request.method in ('POST', 'PUT'):
kwargs.update({
'instance': self.instance,
'data': self.request.POST,
'files': self.request.FILES,
})
return kwargs
def get_instance(self):
""" Add in self.lookups list of attributes to search for instance """
model = self.get_form_class()._meta.model
for lookup in self.lookups:
look = self.kwargs.get(lookup, '')
if look:
try:
self.instance = model.objects.get(*((lookup, look),))
self.pk = self.instance.pk
return self.pk
except ObjectDoesNotExist:
self.instance = None
return 0
|
More like this
- Add context_data to a UpdateView if form is valid by jayfk 2 days, 19 hours ago
- RelatedMixin for Details and Updates with Related Object Lists by christhekeele 12 months ago
- Alternative to Class Based Views by sleepycal 8 months, 1 week ago
- Allow separation of GET and POST implementations by agore 11 months, 1 week ago
- ModelForm-based create_update generic views by carljm 4 years, 10 months ago
Comments