我正在尝试向嵌套的序列化程序显示自定义查询值。但它没有显示
serializer.py
class TueRangeSerializer(serializers.ModelSerializer):
class Meta:
model = SubnetRange
fields = ('type', 'id', 'begin', 'end')
class TueSubnetSerializer(serializers.ModelSerializer):
range = TueRangeSerializer(required=False,read_only=False,many=True)
class Meta:
model = Subnet
fields = ('name','base_address','bits','range')
def create(self,validated_data):
print(validated_data)
在api文件中
api.py
class SubnetList(APIView):
def get(self, request, format=None):
query = 'SELECT ss.id ,ssr.id, ssp.id , ' \
' ss.name as name, ss.bits as bits, ssr.begin as begin FROM subnets_subnet ss' \
' inner join subnets_subnetphysical ssp on ssp.subnet_id = ss.id' \
' inner join subnets_subnetrange ssr on ssr.subnet_physical_id = ssp.id'
subnets = Subnet.objects.raw(query)
serializer = TueSubnetSerializer(subnets,many=True)
return Response(serializer.data)
SQL查询是正确的。但是在序列化程序的响应中,它只显示来自子网表的数据
结果
[
{
"name": "First subnet",
"base_address": "192.100.30.0",
"bits": 24
},
{
"name": "Second subnet",
"base_address": "192.100.30.0",
"bits": 24
}
]
它不显示范围输出