Ok, I apologize. Here is a fuller representation of what I'm doing. I had a
hard time figuring out how much was enough versus too much. This is
simplified, but I think it represents what I'm trying to do. For instance,
don't pay too much attention to the save logic in the view, I haven't
actually really worked on it much yet.
To restate my scenario, I want to carry information in hidden fields of a
form that determine how the form is displayed and functions. I realize I do
have access to the self.Initial values in the form itself but I don't think
that there's easy access to those values in the template. The value that I
want seems to be the value() method of the BoundField, but run through the
to_python() method of the field. I actually solved this by adding a
to_python() method to the BoundField that does just this, but I would like
to solve this without modifying the Django code. The samples below do not
reflect my added 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"].value():
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"].value() and
self["selected"].value()
class Meta:
model = OrganizationItem
# Model classes
class OrganizationItem(models.Model):
organization = models.ForeignKey(Organization)
item = models.ForeignKey(ListItem)
descr = models.TextField("Description", blank=True)
class ListItem(models.Model):
category = models.ForeignKey(ListCategory)
name = models.CharField("Name", max_length=200)
order= models.IntegerField("Order")
extraInfo = models.BooleanField("Requires extra information")
multiLineInfo = models.BooleanField("The display should use a multi-line
edit box instead of a single line edit")
descrLabel= models.CharField("Extra information label",
max_length=50,
blank=True)
class ListCategory(modelUtils.MyModelAudit):
catId = models.CharField("Category identifier", max_length=15,
primary_key=True)
name = models.CharField("Name", max_length=100)
order = models.IntegerField("Order")
group = models.ForeignKey(ListGroup)
organization = models.BooleanField("Should this be associated with an
organization?")
twoColumn= models.BooleanField("Should the current list span 2
columns?")
class ListGroup(modelUtils.MyModelAudit):
name = models.CharField("Name", max_length=15)
descr = models.CharField("Description", max_length=100, blank=True)
order = models.IntegerField("Order")
# View example
def organizationAdd(request):
OrganizationItemFormSet = formset_factory(OrganizationItemForm)
if request.method == 'POST':
orgItemFormSet = OrganizationItemFormSet(request.POST)
orgItemFormSetIsValid = orgItemFormSet.is_valid()
if orgItemFormSetIsValid:
orgItemFormSet.save(commit=False)
else:
orgItemFormSet = OrganizationItemFormSet()
listItems = CreateItemsSection(orgItemFormSet, "organization",
category__organization=True)
context = {
'listItems': listItems,
'listItemsManagementForm': orgItemFormSet.management_form}
return render_to_response('organizationEdit.html', context,
context_instance=RequestContext(request))
def CreateItemsSection(formset, idField="", objId=None, relation=None,
**kwargs):
"""Construct the list of selectable items"""
qs = ListItem.objects.filter(**kwargs)
# Get the appliable list of groups
groups = qs.values(
"category__group", "category__group__descr"
).order_by(
"category__group__order"
).distinct()
# Convert the items relation into a dictionary of ListItem PK and
XItems.descr
selectedData = {}
if relation is not None:
selectedData = dict([(item.item__id, item.descr) for item in
relation])
retVal = []
# Track the current form to display when the formset is populated from a
postback
formIndex = 0;
postedData = len(formset) > 0
# Assemble the information for each category group
for grp in groups:
# The current group's description
curGrpDescr = grp["category__group__descr"]
#