user2062360
user2062360

Reputation: 1543

Filtering rest framework serializer output

I have a rest serializer and i am getting this output(actual data in thousands). I am trying to filter this data such that it displays only the json where a condition is met. (in my case, student.student_id = 29722):

[
    {
        "student": {
            "student_id": 29722, 
            "first_name": "Michaella", 
            "middle_name": "Helene", 
            "last_name": "Bonose", 
            "email": "", 
            "phone": null, 
            "cell_phone": null
        }, 
        "credits_completed": null, 
        "academic_program_gpa": null, 
        "primary_program": true, 
        "academic_program": {
            "id": 595, 
            "acad_program_category": {
                "id": 1, 
                "program_type": {
                    "id": 1, 
                    "program_category_type": "Academic"
                }, 
                "title": "Associate in Arts"
            }, 
            "acad_program_type": {
                "id": 2, 
                "program_type": "Associate Degree"
            }, 
            "acad_program_code": "AA.ARTS", 
            "program_title": "Associate in Arts Degree", 
            "required_credits": 60, 
            "min_gpa": 2.0, 
            "description": ""
        }
    }, 
    {
        "student": {
            "student_id": 29722, 
            "first_name": "Michaella", 
            "middle_name": "Helene", 
            "last_name": "Bonose", 
            "email": "", 
            "phone": null, 
            "cell_phone": null
        }, 
        "credits_completed": null, 
        "academic_program_gpa": null, 
        "primary_program": true, 
        "academic_program": {
            "id": 596, 
            "acad_program_category": {
                "id": 2, 
                "program_type": {
                    "id": 1, 
                    "program_category_type": "Academic"
                }, 
                "title": "Associate in Sciences"
            }, 
            "acad_program_type": {
                "id": 2, 
                "program_type": "Associate Degree"
            }, 
            "acad_program_code": "AS.SCIENCE", 
            "program_title": "Associate in Sciences Degree", 
            "required_credits": 60, 
            "min_gpa": 2.0, 
            "description": ""
        }
    }, .......

Here is my APIView for this:

class StudentAcademicProgramList(APIView):
    def get(self, request, format=None):
        student_academic_program = Student_academic_program.objects.all()
        serialized_Student_academic_program = StudentAcademicProgramSerializer(student_academic_program, many=True)
        return Response(serialized_Student_academic_program.data)

class StudentAcademicProgramDetail(APIView):
    def get_objects(self, pk):
    try:
        return Student_academic_program.object.get(pk=pk)
    except Student_academic_program.DoesNotExist:
        raise Http404

    def get(self, request, pk, format=None):
    student_academic_program = self.get_object(pk)
        serialized_Student_academic_program = StudentAcademicProgramSerializer(student_academic_program)
        return Response(serialized_Student_academic_program.data)

Hoe do i filter this such that it only displays the values where student.student_id = 29722 ?

Upvotes: 0

Views: 65

Answers (1)

schillingt
schillingt

Reputation: 13731

Do you mean to filter StudentAcademicProgramList?

class StudentAcademicProgramList(APIView):
    def get(self, request, format=None):
        # This should be populated however you need it to be.
        student_id = 29722
        student_academic_program = Student_academic_program.objects.filter(student_id=student_id)
        serialized_Student_academic_program = StudentAcademicProgramSerializer(student_academic_program, many=True)
        return Response(serialized_Student_academic_program.data)

Upvotes: 1

Related Questions