from rest_framework import serializers

from devices.models.device import Device, DeviceGroup


class DeviceRelatedField(serializers.PrimaryKeyRelatedField):

    def to_representation(self, device: Device):
        return IDNameDeviceSerializer(device).data


class IDNameDeviceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Device
        fields = ['id', 'name']


class DeviceGroupRelatedField(serializers.PrimaryKeyRelatedField):

    def to_representation(self, device: Device):
        return IDNameDeviceGroupSerializer(device).data

    def use_pk_only_optimization(self):
        return False


class IDNameDeviceGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeviceGroup
        fields = ['id', 'name']

