我有2个与外键关系相关的django应用程序。模型如下:
app1:models.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)
app2:models.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)
我正在尝试实现反向查找,出于某种原因,我无法得到它。我查看了this和this。以下是我从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
模型中状态的值。我如何实现这一目标?