Vote count:
0
I have a django rest application in which i am getting my json in the following format:
[
{
"id": 3684,
"student": 2773951,
"academic_program": 595,
"credits_completed": 28,
"academic_program_gpa": null,
"primary_program": false
},
{
"id": 3685,
"student": 2773951,
"academic_program": 596,
"credits_completed": 26,
"academic_program_gpa": null,
"primary_program": true
}
]
The api view for this is defined as follows:
class StudentAcademicProgramList2(APIView):
def get(self, request, format=None):
student_academic_program = Student_academic_program.objects.filter(student=2773951)
serialized_Student_academic_program = StudentAcademicProgramSerializer2(student_academic_program, many=True)
return Response(serialized_Student_academic_program.data)
def put(self, request, format=None):
pkey = request.DATA
data = [{'id':pkey, 'primary_program': True}]
student_academic_program = Student_academic_program.objects.filter(student=2773951)
serializer = StudentAcademicProgramSerializer2(student_academic_program, data=data, many=True, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status = status.HTTP_201_CREATED)
return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None):
serializer = StudentAcademicProgramSerializer2(data = request.DATA)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status= status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, format=None):
student_academic_program = Student_academic_program.objects.filter(student=2773951)
student_academic_program.delete()
return Response(status = status.HTTP_204_NO_CONTENT)
In my update(PUT) class, it gets a pk value and updates the value of its primary_program to True. I would like to have more functionality here. I want the function to change the primary_program value of the pk value to True and the rest of the primary_program values to false.
ie. I want the function to update the remaining primary_program values in the json to false such that the json always has only one primary_program value as True. How do i achieve this functionality? Please help!
asked 1 min ago
Aucun commentaire:
Enregistrer un commentaire