如果有多个字段与其他模型有关系,并且在序列化程序 Meta 类上指定了 depth>=1,则在为相应 url 生成的示例中,所有关系字段的值都具有相同的对象,例如:
模型:
class User(models.Model):
username = models.CharField(max_length=255)
title = models.CharField(max_length=255)
class OrderType(models.Model):
number = models.IntegerField()
class Order(models.Model):
user = models.ForeignKey('User', on_delete=models.PROTECT)
type = models.ForeignKey('OrderType', on_delete=models.PROTECT)
序列化器:
class OrderSerializer(serializers.ModelSerializer):
class Meta:
model = Order
fields = '__all__'
depth = 1
echema 中的示例:
{
"user": {
"number": 0
},
"type": {
"number": 0
}
}
还有这个警告:
NestedSerializer: Encountered 2 components with identical names "Nested" and different classes <class 'rest_framework.serializers.ModelSerializer.build_nested_field.<locals>.NestedSerializer'> and <class 'rest_framework.serializers.ModelSerializer.build_nested_field.<locals>.NestedSerializer'>. This will very likely result in an incorrect schema. Try renaming one.
但如果不使用深度并且相关字段分配有它们的序列化程序,则模式是正确的:
序列化器:
class OrderSerializer(serializers.ModelSerializer):
user = UserSerializer()
type = OrderTypeSerializer()
class Meta:
model = Order
fields = '__all__'
架构中的示例:
{
"user": {
"username": "string",
"title": "string"
},
"type": {
"number": 0
}
}
使用深度进行嵌套时,如何生成正确的示例?
djangorestframework==3.12.3
drf-spectacular==0.20.2