Django's newforms

by Jeremy Jones

I don't know when it popped up, but as I've been working on a new Django project, I've been noticing the newforms documentation page. Today, I decided to check it out. I am quite impressed with what newforms do. The "old style" "automatic" form handling in Django revolved around database Model object and "manipulators" (which you could create from a Model). Form handling was closely tied to your database model. Indeed, form handling was closely tied to the Django ORM.

New style "automatic" form handling revolves around a Form object. Said Form object is apparently decoupled from the Django ORM. (I suspect this was a necessary step in order to integrate SQLAlchemy and any other non-Django ORM.) Here is an awesome little tutorial on using newforms.

Some of the benefits of using newforms are

  • Less code in your view

  • Less code in your template. I forgot to mention that newforms automatically generate HTML. Including errors! Very nice...

  • Easily swap out form input "widgets".

  • Easily ties to an database model...or not.



If you can't tell, I'm really worked up about newforms. Django just got a whole lot cooler. I'm sure I'll write more on this later.

3 Comments

Adrian Holovaty
2007-01-12 12:35:35
Technically, the old forms system wasn't coupled to the ORM -- it's just that the documentation gave the impression that the model-based forms were the best way to go. That was a failure in documentation more than anything else, but, still, newforms improves the old form API quite a bit. I'm really proud of what we've done with it so far, and it gets better just about every day.
James
2007-01-19 21:04:52
Hopefully this improvement will actually include SQL Alchemy and the possibilities of schema evolution.
Bob
2007-02-22 16:54:23
WOO HOO