Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python get_queryset不适用于drf RetrieveUpdatedStroyapiView_Python_Django_Django Rest Framework - Fatal编程技术网

Python get_queryset不适用于drf RetrieveUpdatedStroyapiView

Python get_queryset不适用于drf RetrieveUpdatedStroyapiView,python,django,django-rest-framework,Python,Django,Django Rest Framework,我正在尝试在RetrieveUpdatedStroyapiView中为我的原始sql查询使用get_queryset函数。ListCreateAPIView()可以正常工作。我是drf的新手。因此,请解释一下是否有什么地方做错了 视图.py class SubnetGatewayDevice(generics.ListCreateAPIView): serializer_class = SubnetDeviceSerializer def get_queryset(self)

我正在尝试在RetrieveUpdatedStroyapiView中为我的原始sql查询使用get_queryset函数。ListCreateAPIView()可以正常工作。我是drf的新手。因此,请解释一下是否有什么地方做错了

视图.py

class SubnetGatewayDevice(generics.ListCreateAPIView):

    serializer_class = SubnetDeviceSerializer

    def get_queryset(self):
        sql_query = " select sd.hostname,sd.ma_number,sd.datacenter_id, ssd.ip, ssp.subnet_id,  " \
                    " sd.id, ssd.id, ssp.id  from subnets_device sd" \
                    " inner join subnets_subnetdevice as ssd on  sd.id = ssd.device_id" \
                    " inner join subnets_subnetphysical as ssp on ssd.subnet_physical_id = ssp.id"
        return list(Device.objects.raw(sql_query))


    def perform_create(self, serializer):
        serializer.save(data=self.request.data)


class SubnetGatewayDeviceDetail(generics.RetrieveUpdateDestroyAPIView):
    # parser_classes = (JSONParser,)

    serializer_class = SubnetDeviceSerializer

    def get_queryset(self):
        device_id = "5"
        sql_query = " select sd.hostname as hostname,sd.ma_number,sd.datacenter_id, ssd.ip, ssp.subnet_id,  " \
                    " sd.id, ssd.id, ssp.id  from subnets_device sd" \
                    " inner join subnets_subnetdevice as ssd on  sd.id = ssd.device_id" \
                    " inner join subnets_subnetphysical as ssp on ssd.subnet_physical_id = ssp.id " \
                    " where sd.id = "+device_id
        return  Device.objects.raw(sql_query)



    # def perform_create(self, serializer):
    #     serializer.save(data=self.request.data)
class SubnetDeviceSerializer(serializers.ModelSerializer):
    subnet_id = serializers.IntegerField(required=True)
    datacenter_id = serializers.IntegerField(required=True)
    ip = serializers.IPAddressField(protocol='both')

    class Meta:
        model = Device
        fields = ('id','hostname','ma_number','subnet_id','datacenter_id','ip')

    def create(self, validated_data):
        subnet_id = validated_data['subnet_id']
        ma_number = validated_data['ma_number']
        hostname = validated_data['hostname']
        data_center_id = validated_data['datacenter_id']
        gateway_ip= validated_data['ip']


        subnet_physical = SubnetPhysical.objects.get(subnet_id=subnet_id)
        if subnet_physical:
            subnet_physical_id = subnet_physical.id

            device_obj = Device()
            device_obj.hostname = hostname
            device_obj.ma_number = ma_number
            device_obj.datacenter_id = data_center_id
            device_obj.save()
            device_pk = device_obj.id

            subnet_device_obj = SubnetDevice()
            subnet_device_obj.ip = gateway_ip
            subnet_device_obj.device_id = device_pk
            subnet_device_obj.subnet_physical_id = subnet_physical_id
            subnet_device_obj.save()

        return device_pk
序列化程序.py

class SubnetGatewayDevice(generics.ListCreateAPIView):

    serializer_class = SubnetDeviceSerializer

    def get_queryset(self):
        sql_query = " select sd.hostname,sd.ma_number,sd.datacenter_id, ssd.ip, ssp.subnet_id,  " \
                    " sd.id, ssd.id, ssp.id  from subnets_device sd" \
                    " inner join subnets_subnetdevice as ssd on  sd.id = ssd.device_id" \
                    " inner join subnets_subnetphysical as ssp on ssd.subnet_physical_id = ssp.id"
        return list(Device.objects.raw(sql_query))


    def perform_create(self, serializer):
        serializer.save(data=self.request.data)


class SubnetGatewayDeviceDetail(generics.RetrieveUpdateDestroyAPIView):
    # parser_classes = (JSONParser,)

    serializer_class = SubnetDeviceSerializer

    def get_queryset(self):
        device_id = "5"
        sql_query = " select sd.hostname as hostname,sd.ma_number,sd.datacenter_id, ssd.ip, ssp.subnet_id,  " \
                    " sd.id, ssd.id, ssp.id  from subnets_device sd" \
                    " inner join subnets_subnetdevice as ssd on  sd.id = ssd.device_id" \
                    " inner join subnets_subnetphysical as ssp on ssd.subnet_physical_id = ssp.id " \
                    " where sd.id = "+device_id
        return  Device.objects.raw(sql_query)



    # def perform_create(self, serializer):
    #     serializer.save(data=self.request.data)
class SubnetDeviceSerializer(serializers.ModelSerializer):
    subnet_id = serializers.IntegerField(required=True)
    datacenter_id = serializers.IntegerField(required=True)
    ip = serializers.IPAddressField(protocol='both')

    class Meta:
        model = Device
        fields = ('id','hostname','ma_number','subnet_id','datacenter_id','ip')

    def create(self, validated_data):
        subnet_id = validated_data['subnet_id']
        ma_number = validated_data['ma_number']
        hostname = validated_data['hostname']
        data_center_id = validated_data['datacenter_id']
        gateway_ip= validated_data['ip']


        subnet_physical = SubnetPhysical.objects.get(subnet_id=subnet_id)
        if subnet_physical:
            subnet_physical_id = subnet_physical.id

            device_obj = Device()
            device_obj.hostname = hostname
            device_obj.ma_number = ma_number
            device_obj.datacenter_id = data_center_id
            device_obj.save()
            device_pk = device_obj.id

            subnet_device_obj = SubnetDevice()
            subnet_device_obj.ip = gateway_ip
            subnet_device_obj.device_id = device_pk
            subnet_device_obj.subnet_physical_id = subnet_physical_id
            subnet_device_obj.save()

        return device_pk

如果需要,您可能需要重写
get\u object()
方法


retrieveUpdatedStroyapiView
在单个实例上执行操作。您可以尝试评估一下,您真正需要实现的目标是什么,以及如何才能更恰当地实现这些目标。

谢谢您的回复。我可以举个例子吗?好的。那么如果我们有一个针对RetrieveUpdatedStroyapiView的自定义查询呢。因为我的输出是多个表的联接,所以您的原始SQL查询似乎正在使用ListCreateAPIView,那么我认为您不需要对RetrieveUpdatedStroyaPiView执行任何操作。您只需将id作为参数传递,其余的将被处理。RetrieveUpdatedStroyapiView可能需要一个属性
model=Device
。我已经尝试过了。甚至原始sql查询也正在打印。但是数据没有传递到序列化程序