将对象正确显示为 Django 表单选择字段中的选项

Show objects properly as choices in django forms choicefield

我正在开发某种测试应用程序。我有两个模型用于测试和用户答案:

class TestModel(AbstractDatetimeModel):
    number = models.IntegerField(unique=True)
    answer_1 = models.CharField(max_length=255)
    answer_2 = models.CharField(max_length=255)

class AnswerModel(AbstractDatetimeModel):
    ANSWERS = (
        ('1', 'A'),
        ('2', 'B')
    )
    candidate = models.ForeignKey(User)
    question = models.ForeignKey(TestModel)
    answer = models.CharField(max_length=1, choices=ANSWERS)

我有一个表格,候选人(用户)可以在其中回答每个问题。为了实现这一点,我使用了一个视图 formset_factory:

def communicative_test(request):
    questions = TestModel.objects.all()
    question_formset = formset_factory(form=TestForm, extra=questions.count())
    question_formset = question_formset(initial=[{'candidate': request.user, 'question': x.number,
                                                  'answer_1': x.answer_1, 'answer_2': x.answer_2} for x in questions])
    return render(request, 'testing/communicative.html', locals())

我需要在表单上显示 ChoiceField,其中选项应该是字段 answer_1 和 answer_2 中的数据。这是这个表格:

class TestForm(forms.ModelForm):
    answer_1 = forms.CharField()
    answer_2 = forms.CharField()
    VARIANTS = (
        ('answer_1', answer_1),
        ('answer_2', answer_2)
    )
    variants = forms.ChoiceField(choices=VARIANTS, widget=forms.RadioSelect())

    class Meta:
        model = TestAnswer
        fields = '__all__'

问题是在页面上,这些答案显示为带有以下标签的单选按钮:

<django.forms.fields.CharField object at 0x7f36248ef5d0>
<django.forms.fields.CharField object at 0x7f36248ef650>

我需要正确显示它。

好吧,正如@Anentropic 提到的,我走错了路。 他是对的,因为我只需要提交答案 A 或 B。但是对于表单上的用户,我需要显示来自测试模型的这些答案的文本。所以我为此写了两个模板标签。 class 测试表格(forms.ModelForm):

    class Meta:
        model = TestAnswer
        fields = '__all__'

def communicative_test(request):
    questions = TestModel.objects.all()
    QuestionFormSet = formset_factory(form=TestForm, max_num=questions.count())
    formset = QuestionFormSet(initial=[{'candidate': request.user, 'question': x.number} for x in questions])
    return render(request, 'testing/communicative.html', locals())

每个答案的两个自定义模板标签 (A/B):

@register.filter(name='get_answer1')
def get_answer1(question):
    try:
        question = TestModel.objects.get(number=question)
        answer = question.answer_1
    except ObjectDoesNotExist:
        answer = ''
    return answer


@register.filter(name='get_answer2')
def get_answer2(question):
    try:
        question = TestModel.objects.get(number=question)
        answer = question.answer_2
    except ObjectDoesNotExist:
        answer = ''
    return answer

模板是:

{% load test_tags %}
<form id="communicative-test-form" method="post">
     {% csrf_token %}
     {{ question_formset.management_form }}
     {% for form in question_formset.forms %}
        {% crispy form %}
        {{ form.id }}
        <p>{{ form.question.value }}</p>
        <p><label>
           <input type=radio name="form-{{ form.question.value|add:"-1" }}-answer" id='id_form-{{ form.question.value|add:"-1" }}-answer' value="1">
                        {{ form.question.value|get_answer1 }}
           </label></p>
           <p><label>
              <input type=radio name="form-{{ form.question.value|add:"-1" }}-answer" id='id_form-{{ form.question.value|add:"-1" }}-answer' value="2">
                        {{ form.question.value|get_answer2 }}
              </label></p>
      {% endfor %}
      <div style="text-align: right;" class="col-md-12">
          <button type="submit">Save</button>
      </div>
</form>