Django REST Framework Serializer返回对象而不是数据

时间:2016-09-25 15:57:02

标签: python json django django-rest-framework

我正在为我居住的公寓写一个简单的数据库,其中包含人员,单位,单位类型(家庭与停车位)和单位持有人的列表(连接表,用于人与人之间的多对多关系)单位) - 一个人在租用停车位时可以是单位类型“房屋”的所有者。

这是我的模特:

class Person(models.Model):
    first_name = models.CharField(max_length=30, null=False)
    last_name = models.CharField(max_length=30, null=False)
    phone = models.CharField(max_length=20)
    email = models.EmailField(max_length=20)

class UnitType(models.Model):
    description = models.CharField(max_length=30)

class Unit(models.Model):
    unit_number = models.IntegerField(null=False, unique=True)
    unit_type = models.ForeignKey(UnitType, null=False)
    unitholders = models.ManyToManyField(Person, through='UnitHolder')

class UnitHolderType(models.Model):
    description = models.CharField(max_length=30)

class UnitHolder(models.Model):
    person = models.ForeignKey(Person)
    unit = models.ForeignKey(Unit)
    unitholder_type = models.ForeignKey(UnitHolderType)

这是我的观点:

class PersonViewSet(viewsets.ModelViewSet):
    queryset = Person.objects.all()
    serializer_class = PersonSerializer

class UnitHolderTypeViewSet(viewsets.ModelViewSet):
    queryset = UnitHolderType.objects.all()
    serializer_class = UnitHolderTypeSerializer

class UnitViewSet(viewsets.ModelViewSet):
    queryset = Unit.objects.all()
    serializer_class = UnitSerializer

class UnitHolderViewSet(viewsets.ModelViewSet):
    queryset = UnitHolder.objects.all()
    serializer_class = UnitHolderSerializer

class UnitTypeViewSet(viewsets.ModelViewSet):
    queryset = UnitType.objects.all()
    serializer_class = UnitTypeSerializer

这是我的序列化器:

class UnitSerializer(serializers.ModelSerializer):
    unit_type = serializers.SlugRelatedField(
        queryset=UnitType.objects.all(), slug_field='description'
    )

    class Meta:
        model = Unit
        fields = ('unit_number', 'unit_type', 'unitholders')

class UnitTypeSerializer(serializers.ModelSerializer):

    class Meta:
        model = UnitType

class PersonSerializer(serializers.ModelSerializer):
    class Meta:
        model = Person

class UnitHolderSerializer(serializers.ModelSerializer):
    person = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
    unit = serializers.PrimaryKeyRelatedField(many=False, read_only=True)

    class Meta:
        model = UnitHolder
        fields = ('person', 'unit', 'unitholder_type')

class UnitHolderTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = UnitHolderType

问题:

当我查询/ units端点时,如下所示:

u = requests.get('http://localhost:8000/units').json()

我的回答如下:

[{'unit_type': 'Home', 'unit_number': 614, 'unitholders': [1]}]

我想要的是这样的:

[
    {
        'unit_type': 'Home', 
        'unit_number': 614, 
        'unitholders': [
            {
                'id: 1,
                'first_name': 'myfirstname',
                'last_name': 'mylastname',
                'unitholder_type': 'renter'
            }
        ]
    }
]

我很确定我的问题出现在我的UnitSerializer中,但我是DRF的新手并通过文档阅读但仍然无法弄明白。

2 个答案:

答案 0 :(得分:4)

一个简单的解决方案是使用depth选项:

class UnitSerializer(serializers.ModelSerializer):
    unit_type = serializers.SlugRelatedField(
        queryset=UnitType.objects.all(), slug_field='description'
    )

    class Meta:
        model = Unit
        fields = ('unit_number', 'unit_type', 'unitholders')
        depth = 1

这会将所有嵌套关系序列化为1级深度。如果要对每个嵌套字段的序列化方式进行精确控制,可以明确列出其序列化程序:

class UnitSerializer(serializers.ModelSerializer):
    unit_type = serializers.SlugRelatedField(
        queryset=UnitType.objects.all(), slug_field='description'
    )
    unitholders = UnitHolderSerializer(many=True)

    class Meta:
        model = Unit
        fields = ('unit_number', 'unit_type', 'unitholders')

另外作为旁注,您需要考虑将视图中的查询集修改为prefetch related objects,否则您将非常快速地破坏应用程序性能(使用类似django-debug-toolbar的内容来监视生成的查询非常方便):

class UnitViewSet(viewsets.ModelViewSet):
    queryset = Unit.objects.all().select_related('unit_type').prefetch_related('unitholders')
    serializer_class = UnitSerializer

答案 1 :(得分:0)

也许你必须这样做:

class UnitHolderViewSet(viewsets.ModelViewSet):
    queryset = UnitHolder.objects.all()
    unitholders = UnitHolderSerializer(read_only=True, many=True)

Django rest framework serializing many to many field