[英]Django accessing foreign key value from api
According to the DRF documentation here , in the class-based views, we can send a value argument using a keyword pk
. 根据此处的DRF文档,在基于类的视图中,我们可以使用关键字
pk
发送值参数。 I am trying to pass 2 values to the GET function in the following code. 我试图在下面的代码中将2个值传递给GET函数。
example: 例:
class studentList(APIView):
def get(self, request, pk, pk2, format=None):
student_detail = Student.objects.filter(last_name = pk, campus_id.name = pk2)
serialized_student_detail = studentSerializer(student_detail, many=True)
return Response(serialized_student_detail.data)
In the above, the campus_id
is a foreign key relation to another model and hence it is returning me an error. 在上面,
campus_id
是与另一个模型的外键关系,因此返回错误。 How can we access the vlues of the foreign key here? 我们如何在这里访问外键的伪装? In my serializer, i mentioned the depth to be 1 so that it fetches the values from the foreign key relationship.
在我的序列化程序中,我提到了深度为1,以便它从外键关系中获取值。 How do i do it?
我该怎么做?
使用双下划线访问ForeignKey关系属性
student_detail = Student.objects.filter(last_name=pk, campus__name=pk2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.