belek
belek

Reputation: 957

Show objects properly as choices in django forms choicefield

I am working on some kind of testing application. I have two models for tests and users answers:

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)

And I have a form, where candidate (user) can answer each question. To implement this I use a view 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())

On a form I need to show ChoiceField, where choices should be data from fields answer_1 and answer_2. Here is this form:

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__'

Problem is that on a page these answers displays as radio buttons with following labels:

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

I need to display it properly.

Upvotes: 0

Views: 1516

Answers (1)

belek
belek

Reputation: 957

Well, as @Anentropic mentioned, I was not on the right way. And he is right, because I need to submit only answer A or B. But for user on the form I need to display text for these answers from Test model. So I wrote two templatetags for this. class TestForm(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())

Two custom templatetags for each answer (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

And template is:

{% 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>

Upvotes: 1

Related Questions