Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including generated fields to Django JSON serialization

I have a django app where i have an api defined as follows:

Here is what is what i am sending in my json from my api.py:

class studentList(APIView):
    def get(self, request, pk, format=None):
        student_detail = Student.objects.filter(student_id = pk)
        serialized_student_detail = studentSerializer(student_detail, many=True)
        return Response(serialized_student_detail.data)

I have another variable named status which i am calculating with the help of reverse lookups. What i am trying to achieve is to send another field named status along with the above json values. Since my model does not have a status field in it, How do i achieve this?

like image 506
Abhishek Avatar asked Jul 16 '26 09:07

Abhishek


1 Answers

What you may need is a SerializerMethodField (http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield). Note that by default it is a read only field.

like image 176
Pierre Alex Avatar answered Jul 18 '26 23:07

Pierre Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!