0

I want to create a custom API view based on existing data.

models.py

class Practice(models.Model):
    practice_id = models.BigAutoField(primary_key=True)
    score = models.SmallIntegerField(null=True)
    correct = models.SmallIntegerField(null=True)
    wrong = models.SmallIntegerField(null=True)
    not_answered = models.SmallIntegerField(null=True)

    class Meta:
        managed = True
        db_table = 'practice'

    def __str__(self):
        return str(self.practice_id)

serializers.py

class PracticeSerializer(serializers.ModelSerializer):

    class Meta:
        model = Practice
        fields = ('practice_id',
                  'score',
                  'correct',
                  'wrong',
                  'not_answered',
                  )

views.py

@api_view(['GET'])
def practice_detail(request, pk):
    try: 
        practice = Practice.objects.get(pk=pk) 
    except Practice.DoesNotExist: 
        return JsonResponse({'message': 'The practice does not exist'}, status=status.HTTP_404_NOT_FOUND) 

    if request.method == 'GET': 
        exercises_serializer = PracticeSerializer(practice) 
        return JsonResponse(exercises_serializer.data)

With the code above I get the results of the API view as below using practice id 485 :

/api/practice/485

{
    practice_id: 485,
    score: 10,
    correct: 2,
    wrong: 3,
    not_answered: 0,
}

Now I want to create a custom API view with the result as below :

{
    labels: ["Practice"],
    datasets: [
        {
            label: "Correct",
            data: [2],
        },
        {
            label: "Wrong",
            data: [3],
        },
        {
            label: "Not_answered",
            data: [0],
        }
    ]
}

How to do that?

Is possible to achieve that without create new models?

1 Answer 1

1

Define the format in Serializer.to_representation() as documented

.to_representation() - Override this to support serialization, for read operations.

class PracticeSerializer(serializers.ModelSerializer):

    class Meta:
        model = Practice
        fields = ('practice_id',
                  'score',
                  'correct',
                  'wrong',
                  'not_answered',
                  )

    def to_representation(self, instance):
        """
        Object instance -> Dict of primitive datatypes.
        """
        return {
            "labels": ["Practice"],
            "datasets": [
                {
                    "label": "Correct",
                    "data": instance.correct,
                },
                {
                    "label": "Wrong",
                    "data": instance.wrong
                },
                {
                    "label": "Not_answered",
                    "data": instance.not_answered,
                }
            ]
        }

Output:

$ curl http://127.0.0.1:8000/api/practice/485
{"labels": ["Practice"], "datasets": [{"label": "Correct", "data": 2}, {"label": "Wrong", "data": 3}, {"label": "Not_answered", "data": 0}]}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.