为什么 django-rest-framework 不显示 OneToOneField 数据 - django
Why django-rest-framework doesn't display OneToOneField data - django
我想在我的站点中设置 restful API。我使用了 django-rest-framework。
当我从数据库中获取一个对象时,它没有显示相关对象。
下面的片段是我的第一个模型(父模型):
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='profile',on_delete=models.CASCADE)
name = models.CharField(max_length=30)
family = models.CharField(max_length=50)
下面的片段是我的第二个模型(子模型):
class Klass(models.Model):
title = models.CharField(max_length=50)
description = models.CharField(max_length=500)
teacher = models.ForeignKey(Profile,related_name='teacher', on_delete=models.CASCADE)
university = models.CharField(max_length=50, blank=True, null=True)
如您在第二个片段中所见,teacher
从 Profile
模型中获取其值。
但是在 django-rest-framework API 视图中,显示的不是教师的名字,而是 pk
.
下面的片段是我的序列化器和视图:
# serializer
class KlassSerializer(serializers.ModelSerializer):
class Meta:
model = Klass
fields = ('id', 'title', 'description', 'teacher')
# view
class KlassView(APIView):
def get(self, request, pk=None):
if pk is not None:
klass = Klass.objects.filter(pk=pk).get()
serializer = KlassSerializer(klass)
return Response({'message': 'class get ', 'data': serializer.data,})
这是结果:
{
"message": "class get ",
"data": {
"id": 13,
"title": "ُThe First Class",
"description": "Nothing for now!",
"teacher": 2
}
}
我该如何解决这个问题?
谢谢
试试看,更详细nested-relationships
class TeacherSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('name')
class KlassSerializer(serializers.ModelSerializer):
teacher = TeacherSerializer(read_only=True)
class Meta:
model = Klass
fields = ('id', 'title', 'description', 'teacher')
我想在我的站点中设置 restful API。我使用了 django-rest-framework。 当我从数据库中获取一个对象时,它没有显示相关对象。
下面的片段是我的第一个模型(父模型):
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='profile',on_delete=models.CASCADE)
name = models.CharField(max_length=30)
family = models.CharField(max_length=50)
下面的片段是我的第二个模型(子模型):
class Klass(models.Model):
title = models.CharField(max_length=50)
description = models.CharField(max_length=500)
teacher = models.ForeignKey(Profile,related_name='teacher', on_delete=models.CASCADE)
university = models.CharField(max_length=50, blank=True, null=True)
如您在第二个片段中所见,teacher
从 Profile
模型中获取其值。
但是在 django-rest-framework API 视图中,显示的不是教师的名字,而是 pk
.
下面的片段是我的序列化器和视图:
# serializer
class KlassSerializer(serializers.ModelSerializer):
class Meta:
model = Klass
fields = ('id', 'title', 'description', 'teacher')
# view
class KlassView(APIView):
def get(self, request, pk=None):
if pk is not None:
klass = Klass.objects.filter(pk=pk).get()
serializer = KlassSerializer(klass)
return Response({'message': 'class get ', 'data': serializer.data,})
这是结果:
{
"message": "class get ",
"data": {
"id": 13,
"title": "ُThe First Class",
"description": "Nothing for now!",
"teacher": 2
}
}
我该如何解决这个问题? 谢谢
试试看,更详细nested-relationships
class TeacherSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('name')
class KlassSerializer(serializers.ModelSerializer):
teacher = TeacherSerializer(read_only=True)
class Meta:
model = Klass
fields = ('id', 'title', 'description', 'teacher')