Friday, January 20, 2012

Groups in Forms

I have a Jquery Notebook that has common options on one tab and more
advanced options on another tab.

The model referencing this data has both tabs of data and a
description field as well.

Initially, I broke out the form manually, but I decided that this
would be a major pain to write <tr><td>{{ formfield.label }}:</
td><td>{{ formfield }}</td></tr> for each and every single row, not to
mention to add in the custom error class for the field when it fails
to validate.

Then I decided to have three forms, one for each tab and a single
field form for description. But, in using forms.Forms you lose
Forms.save() and other shortcuts. So, to save back to an existing
model, I think, you need to break out each one again.

example:

m = models(pk=mypk)

m.date = form.cleaned_data['date']

and then you would have to do that for each line. This seems
inefficient to me.

For now, I'm using two model forms, excluding the fields of the
opposite tab in the modelform.

Example:

class TabOne(ModelForm):
class Meta:
model = models.models
exclude = ('stuff_from_tab_two',
'stuff_from_tab_two',
'stuff_from_tab_two',
'stuff_from_tab_two')

and then again for TabTwo.

My question is:

Is it possible to break out a ModelForm into groups for display
purposes? It would be handy if in the Meta you could set a "break"
attribute that allowed you to break out different areas of the same
ModelForm

For example, in a model one might have:

Class Model(models.Models):
field_1 = models.IntegerField()
field_2 = models.IntegerField()
field_3 = models.IntegerField()
field_4 = models.IntegerField()
field_5 = models.IntegerField()
field_6 = models.IntegerField()
field_7 = models.IntegerField()
field_8 = models.IntegerField()
field_9 = models.IntegerField()
field_10 = models.IntegerField()

in the ModelForm:

class Form(ModelForm):
class Meta:
break = (('one',field_3),
('two',field_7),
('three',field_10),)


form = Form()
then to refer to form in a template, where you defined it as
'form':form, in the Context, it would be form['form_one'],
form['form_two'], form['form_three'].

Basically, the break would create a dictionary appending the name you
gave in the first part of the tuple to the name of the form.

Or anyway, something like that.

For now, I can't see a simple way to break out groups of a form. It
seems you have to either use the whole thing as intended or break it
up into individual parts in the template.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment