Django Forms Newbie Question

前端 未结 2 1729
花落未央
花落未央 2021-02-03 15:47

Alright, I\'m at a loss with the Django Forms, as the documentation just doesn\'t seem to quite cover what I\'m looking for. At least it seems to come to a screeching halt once

2条回答
  •  野的像风
    2021-02-03 16:29

    Yeah I have to agree the documentation and examples are really lacking here. The is no out of the box solution for the case you are describing because it goes three layers deep: quiz->question->answer.

    Django has model inline formsets which solve the problem for two layers deep. What you will need to do to generate the form you want is:

    1. Load up a quiz form (just a label text box from your model)
    2. Load a an question formset: QuestionFormSet(queryset=Question.objects.filter(quiz=quiz))
    3. For each question load up a answer formset in much the same way you load up the question formset
    4. Make sure you save everything in the right order: quiz->question->answer, since each lower level needs the foreign key of the item above it

提交回复
热议问题