views:

558

answers:

2

There are a lot of complex forms in my project and I keep getting the feeling that I could be coding them much more elegantly and simply. So my question is what are some good apps and practices that might help me? Specifically, I'm thinking about situations when I need to do stuff like:

  • edit/add more than one object via one form (example: Lets say I have a Partnership model and a Person model - every partnership object is related to two people. Now lets say I want to edit the partnership and the two people in the partnership simultaneously.)
  • deal with many to many relationships - particularly those that have extra data associated
  • "wizard-like" forms (as in there's a couple of pages/steps and the user has to get through all of them before anything gets saved to the database)
  • giving suggestions for what to write into a form field based on what's in the database (I guess this is an AJAX question really, but I'm interested in whether there are some django apps that simplify this somehow)

Solutions to any other more complex form scenarios also welcome. The above are problems I've already come across, but I'd like to generally find out about what are some best practices with forms.

+5  A: 

I've asked similar questions myself. There's a lot of room for improvement in Django's form handling, and there is some discussion of doing something about that but it isn't moving very quickly. Your asking this question validates this area needs some attention.

That said, the current ways (to the best of my knowledge) to handle those scenarios is:

  1. The easiest way is to use a ModelForm for the Partnership and inlineformset for the People. It isn't too difficult, but be aware that there's no easy way to combine them into one form object so you'll have to handle them separately.
  2. Not exactly sure what scenario you have in mind, but I think inline formsets are the way to go here as well.
  3. Django has support for this as of 1.0: http://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/
  4. I'm working on an app to do exactly this (with help from jQuery). I've been meaning to clean it up and put it on GitHub, and any extra interest will certainly speed up that process.

Inline Formsets: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets

John Debs
To expand on #4, it's pretty simple. You just need a django view you can pass some params to with an ajax call. The django view returns some json data with the suggestions. You should be able to use pretty much any jquery (or other js framework) autocomplete/hinting plugin with a django view like that.
Alex Jillard
Hmmm... the annoying thing about form-wizard is that there doesn't seem to be a way to make it dynamic (change the steps based on previous input).
Monika Sulik
@Monika - actually there *is* a way to customize the wizard. You have to swap its `process_step()` method and override next forms there, based on input from current one.
Tomasz Zielinski
A: 

Some form discoveries I've made since asking this question... I'll be editing this as/if I find out more.

Autocomplete

There actually already is an application that does this called django-autocomplete. Haven't tried it out yet though...

Combining a few forms into one

This is a pretty interesting solution as to how to edit more than one object without using more than one form, but again haven't tried this out yet.

Monika Sulik