This mail maybe can be splitted in two, but I write only one because
both are related.

Ok, ModelForms is a very very wonderful thing, but I want to talk
about (maybe) excessive implicitness.

Look at this form declaration:

class AuthorForm(forms.Form):
    name = myforms.MyCharField(max_length=100)
    email = myforms.MyEmailField()

What is the first you find out? Ok, it's a forms with two fields.

Now look at this:

class AuthorForm(forms.ModelForm):
    name = myforms.MyCharField(max_length=100)
    email = myforms.MyEmailField()

    class Meta:
        model = Author

Now, what do you see? You first think again in a form with two fields,
however maybe is a form with five fields. What is happening? You
explicity define two fields, and the real action is a _redefinition_
of two fields.

Django philosophy talk about "Explicit better than implicit", but in
this point ModelForms is not very intuitive.

I don't know if exists a better solution for this. A colleage mine
develops (before implementation of ModelForms) a form wrapper solution
that now we published [1], with ideas from Zope3 formlib, with a
FormField class for collecting fields of a model (another way to
implement something like ModelForms).

After ModelForms was implemented, we've migrated this wrapper to
ModelForms [2], because now is the best practice, but I missed a mixed
solution, using something like FormFields class.

On the other hand (now begin second part of mail), I want to talk
about cmsutils/forms.py file. I think GenericAddForm [3] and
GenericEditForm [4] implementation is a good way to simplify 90% of
your Django views. Typical form management view would be:

def author_edit(request, author_slug):
    author = get_object_or_404(Author, slug=author_slug)
    if request.method == 'POST':
        form = AuthorEditForm(request.POST, instance=author)
        if form.is_valid():
            new_author = form.save()
            return HttpResponseRedirect(new_author.get_absolute_url())
    else:
        form = AuthorEditForm(instance=author)
    return render_to_response('authors/edit.html', {'form': form} )

But if you define AuthorEditForm extending GenericEditForm like this,
views are simpler:

class AuthorEditForm(GenericEditForm):
    template = 'authors/edit.html'
    class Meta:
         ... # the same as usual

def author_edit(request, author_slug):
    author = get_object_or_404(Author, slug=author_slug)
    form = AuthorEditForm(request, author)
    return form.run()

form.run() call if get, render template, if post, validate and
redirect or render (depends on validation errors). Ok, maybe now _this
solution is less explicit_, but I think less code advantage is
important (70% less code in our projects views). Besides, you always
can use raw ModelForms :-)

¿Do you think something like Generic*Form can be needed in Django
core?

¿Any opinion, criticism or suggestion for both parts of email?

Regards,
Lin

[1] https://tracpub.yaco.es/cmsutils/browser/trunk/forms.py?rev=1#L216
[2] https://tracpub.yaco.es/cmsutils/browser/trunk/forms.py
[3] https://tracpub.yaco.es/cmsutils/browser/trunk/forms.py#L54
[4] https://tracpub.yaco.es/cmsutils/browser/trunk/forms.py#L85
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to