简体   繁体   中英

get_queryset not working for drf RetrieveUpdateDestroyAPIView

I am trying using get_queryset function for my raw sql query in the RetrieveUpdateDestroyAPIView . This is working fine with ListCreateAPIView(). I am new to drf . So could please explain if there is anything doing wrong.

views.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)

Serializer.py

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

You may need to override get_object() method, if you need.

RetrieveUpdateDestroyAPIView does the operations on a single instance. You may try to evaluate for a moment, what it is that you really need to achieve and how can it be done more appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM