1

Here's the code.

models.py

class Question(models.Model):
    lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, related_name='questions')
    question = models.CharField('lesson name', max_length=120)


class Option(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='options')
    option = models.CharField('test option', max_length=100)
    correct = models.BooleanField(default=False)

views.py

class QuestionAPIView(generics.CreateAPIView):
    queryset = Question.objects.all()
    serializer_class = QuestionSerializer

serializers.py

class OptionSerializer(serializers.ModelSerializer):

    class Meta:
        model = Option
        fields = ('option', 'correct')


class QuestionSerializer(serializers.ModelSerializer):
    options = OptionSerializer(many=True)

    class Meta:
        model = Question
        fields = ('lesson', 'question', 'options')

    def create(self, validated_data):
        options_data = validated_data.pop('options')
        question = Question.objects.create(**validated_data)
        for option_data in options_data:
            Option.objects.create(question=question, **option_data)
        return question

I'm trying to create a test question with a few options for answers like this:

data = {
    "lesson": 2,
    "question": "This is our first question?",
    "options": [
        {
            "option": "Option 1",
            "correct": True
        },
        {
            "option": "Option 2 ",
            "correct": False
        },
        {
            "option": "Option 3",
            "correct": True
        }
    ]
}

But in postman and in the TestCase I've got:

{
    "options": [
        "This field is required."
    ]
}

What is the problem of validation?

Here's the Documentation:

https://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Anna Berezko
  • 49
  • 1
  • 6
  • 1
    The problem was in postman, how correctly send the array data, the answer is here https://stackoverflow.com/questions/12756688/is-it-possible-to-send-an-array-with-the-postman-chrome-extension. Send your data separately like: options[0]option, options[0].correct. And I'm still looking how can i test this with API TestCase. – Anna Berezko Dec 21 '22 at 10:25

1 Answers1

0

It does work perfectly in my testing. Just note your JSON object, you used True / False as Python boolean varible not JSONs: (Tested both with postman and DRF Browsable API)

data = {
    "lesson": 2,
    "question": "This is our first question?",
    "options": [
        {
            "option": "Option 1",
            "correct": true
        },
        {
            "option": "Option 2 ",
            "correct": false
        },
        {
            "option": "Option 3",
            "correct": true
        }
    ]
}
Niko
  • 3,012
  • 2
  • 8
  • 14