I realize that I went from too little information, to too much information
in the previous post, so this is an attempt to find a middle ground.
What I'm building is the ability to have a list of checkable options, and
depending on the setup for a give option it may have a text field to enter
additional information. This text field can be either displayed as a single
line text input or a multi-line textarea.
My problem is that I want to have these different setup values available to
control the validation and display of the form, but there doesn't seem a
consistent way to test the values. When the form is POSTed back and isn't
valid, the value out of the BoundField's value() metod for BooleanFields is
a string, where it's a actual boolean on the initial creation of the page. I
understand why this is, it's pulling from the POST data instead of the
initial data.
The way I over came this was to add a to_python() method to the BoundField
class in django/forms/forms.py:
def to_python(self):
return self.field.to_python(self.value())
Below is what I'm doing. I'm using the new to_python() method are in the
__init__ of the form, the save logic of the view, and the template at the
end. I left out the CreateItemsSection() function that builds of the
listItems dict because its convoluted. The listItems structure is in a form
that's easily looped through in the template and the function creates (on
the initial load) or inserts (on POST back) the forms of the formset. If it
would be helpful I can post it but I think there should be enough
information here.
Is there a better way to accomplish what I'm doing with the to_python()
method?
# Form class
# I use the self["multiLineInfo"] pattern instead of
self.fields["multiLineInfo"]
# because the former gives a BoundField which merges the Field and the
# post/initial data
class OrganizationItemForm(ModelForm):
selected = forms.BooleanField(required=False)
extraRequired = forms.BooleanField(required=False,
widget=forms.HiddenInput)
multiLineInfo = forms.BooleanField(required=False,
widget=forms.HiddenInput)
def __init__(self, *args, **kwargs):
super(AuditModelForm, self).__init__(*args, **kwargs)
if self["multiLineInfo"].to_python():
self.fields["descr"].widget =
forms.Textarea(attrs={"class":"descriptionEdit"})
else:
self.fields["descr"].widget =
forms.TextInput(attrs={"class":"itemDescrEdit"})
self.fields["descr"].required = self["extraRequired"].to_python()
and self["selected"].to_python()
class Meta:
model = OrganizationItem
# Model classes
class OrganizationItem(models.Model):
organization = models.ForeignKey(Organization)
item = models.ForeignKey(ListItem)
descr = models.TextField("Description", blank=True)
# View
def organizationAdd(request):
OrganizationItemFormSet = formset_factory(OrganizationItemForm)
if request.method == 'POST':
form = OrganizationForm(request.POST)
orgItemFormSet = OrganizationItemFormSet(request.POST)
if form.is_valid():
form.save()
for itm in orgItemFormSet:
if itm["selected"].to_python():
itm.instance.organization = form.instance
if orgItemFormSet.is_valid():
SaveItems(form.instance, orgItemFormSet)
redirect("orgEdit", form.instance.pk)
else:
form.instance.delete()
listItems = CreateItemsSection(orgItemFormSet,
category__organization=True)
else:
form = OrganizationForm()
orgItemFormSet = OrganizationItemFormSet()
listItems = CreateItemsSection(orgItemFormSet, "organization",
category__organization=True)
context = {
'title': 'New organization',
'form': form,
'listItems': listItems,
'listItemsManagementForm': orgItemFormSet.management_form,
'isNew': True}
return render_to_response('organizationEdit.html', context,
context_instance=RequestContext(request))
# Template
{{ listItemsManagementForm }}
{% for item in listItems %}
{{ item.form.descr.errors }}{% if item.form.descr.errors
%}{% endif %}
{{ item.form.selected }} {{ item.label }}
{% if item.form.extraRequired.to_python or item.descrLabel %}
{% if item.form.multiLineInfo.to_python %}
{% else %}
{% endif %}
{{ item.descrLabel }}
{% if item.form.multiLineInfo.to_python %}
{% else %}
{% endif %}
{{ item.form.descr }}
{% else %}