Hello group,
I'm trying to build a fairly complicated form submission using
newforms, which requires that the form is modified based on
information in the model.
For example, I've got a couple of models which look like this:
class Question(models.Model):
questionText = models.CharField(maxlength=255,
verbose_name="question text")
classification = models.ForeignKey('Classification')
exampleAnswer = models.CharField(maxlength=255,
verbose_name="example answer")
required =
models.BooleanField(verbose_name="compulsory question?")
class Answer(models.Model):
question = models.ForeignKey('Question')
classification = models.ForeignKey('Classification')
job = models.ForeignKey('Job')
answerText = models.TextField()
The basic premise is that each Classification has a set of Questions
associated with it, some of which are required. So the form is really
simple - there's only one editable field so the user can answer the
question:
class QuestionAnswerForm(forms.Form):
text = forms.CharField(widget=forms.Textarea, help_text="replace
me with the example answer", verbose_name="replace me with the
question")
I'd like to set the verbose_name on that CharField to be the question
to be answered, and the help_text to be the example answer; similarly
for whether the field is required or not. This can be done by
modifying form.fields['fieldname'], but unfortunately the changes
don't remain after the forms have been POSTed, which I think is
because the form is rebuilt from the POSTed information.
You can also modify form.base_fields, but those changes are applied on
a class-wide (as opposed to per-instance) basis, which doesn't help me
because I have multiple Question forms on each page.
Short of including the extra context information (example answer,
required etc.) inside the form in hidden fields, I'm stuck.
Can anyone suggest a better way of doing what I'm trying to achieve?
Many thanks,
Matt.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---