Django在api中用json值发送反向查找值

2024-04-27 01:03:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个django应用程序与外键关系有关。模型如下:

附录1:型号.py

from django.db import models
from app2.models import Student

class Profile(models.Model):
    profile_id = models.IntegerField(primary_key=True)
    student_id = models.ForeignKey('app2.Student', null=True, blank=True)
    status = models.CharField(max_length=150, null=True, blank=True)

附录2:型号.py

from django.db import models
from app1.models import Profile

class Student(models.Model):
    student_id = models.IntegerField(primary_key=True)
    first_name = models.CharField(max_length=150, null=True, blank=True)
    last_name = models.CharField(max_length=150, null=True, blank=True)

我正在尝试实现反向查找,由于某些原因,我无法得到它。我看过thisthis。以下是我在我的api.py中用json发送的内容:

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)

我试图实现的是发送另一个名为status的字段以及上述json值,这些值将包含Profile模型中的status值。我如何做到这一点?你知道吗


Tags: djangofrompyimportidtruemodelsstatus