I really liked the two, now deprecated, methods (form_for_model and
form_for_instance) for working with newforms. I could reuse the dead-
simple logic (if post: validate and save else: display once again) and
pass the generated Form class in both add and edit. Let me show you:
def processForm(ProjectForm, context={}):
if request.method == 'POST':
form = ProjectForm(request.POST)
if form.is_valid():
project = form.save()
return HttpResponseRedirect(project.absolute_link)
else:
form = ProjectForm()
context['form'] = form
return render_to_response('project_editdetails.html', context)
The modelforms, however, don't have that feature. I nees to set the
instance, or the initial data every time I instanciate a form. I wrote
a quick fix to get the behaviour I expected.
def newform_with_options(SomeForm, **outer_kwargs):
class Wrapper(SomeForm):
def __init__(self, *args, **kwargs):
kwargs.update(outer_kwargs)
SomeForm.__init__(self, *args, **kwargs)
Wrapper.__name__ = SomeForm.__name__ + 'ForInstance'
return Wrapper
Now I can write this in my view:
FormForInstance = newform_with_options(MyModelForm,
instance=some_model)
return processForm(FormForInstance)
I'm wondering though - if it was as good as I think, it would get to
the trunk already in form of such a function or something like
MyModelForm.variant_with(instance=instance).
I'm very curious of your opinions.
Konrad Delong
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---