过滤休息框架序列化程序输出

时间:2014-04-29 16:49:18

标签: django django-rest-framework

我有一个休息序列化器,我得到这个输出(实际数据以千计)。我试图过滤这些数据,使其只显示满足条件的json。 (就我而言,student.student_id = 2972​​2):

[
    {
        "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": ""
        }
    }, .......

这是我的APIView:

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我是否会对此进行过滤,以便仅显示student.student_id = 29722

的值

1 个答案:

答案 0 :(得分:1)

您的意思是过滤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)
相关问题