Felix,
I'm new to Python and Django, so others may be able to improve
on this, but I had a similar scenario recently and this was my
solution.
I used 2 ModelForms in the same HTML template. My view method
looks something like (edited to use your question/answer instead
of my original donor/donation objects):
def question_and_answer(request):
"""
Show blank question and answer form, or save question and answer,
or re-show form with errors.
Handles 3 cases:
- Show empty editable form for new questions and answer (GET)
- Save details for new question and answer (POST, valid form)
- Show errors/re-prompt for new question and answer (POST, invalid
form)
Does not allow edit of existing question or answer.
"""
if request.method == 'POST':
answerform = AnswerForm(request.POST)
questionform = QuestionForm(request.POST)
if questionform.is_valid() and answerform.is_valid():
questionform.save()
answerform.instance.question = questionform.instance
answerform.save()
return HttpResponseRedirect( ... )
else:
answerform = AnswerForm()
questionform = QuestionForm()
return render_to_response(
'question_and_answer.html',
{'answerform': answerform,
'questionform': questionform,
},
context_instance=RequestContext(request)
)
You'll have to enhance the above to support multiple answers.
Also, to allow editing of existing questions and answers. I have
code for that scenario too, if you can't figure it out, but this
should get you started.
Anyone else have improvements on this?
--Fred
------------------------------------------------------------------------
Fred Stluka -- mailto:f...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
------------------------------------------------------------------------
On 10/6/12 5:28 PM, Felix Schlitter wrote:
Hello,
I have a pretty basic problem that I cannot find any reference for to
solve. Let's say I have the following relationship scenario: *question
*and *answer*, where *each* *question* may have up to *four answers*,
but *each* *answer* can only have *one question*. I concluded to put a
/ForeignKey/ on the answer model.
The problem, however, comes when I want to add a new question.
Currently, I would need to first add a question by itself and
afterwards create new answers and reference the question. Optimally, I
would like to be able to create the necessary answers right from the
question form and associate them with the question upon saving. This
of course also implies that upon load a form for a certain question,
the already associated answers will be loaded.
Do I handle the form logic in a ModelForm subclass, on the view or is
there already pluggable app for this that I missed?
Seems like this scenario would be a pretty common problem,
Felix
--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/ikvVkFXPhKUJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
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+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.