from rest_framework.serializers import ModelSerializer

from .models import Equipment
from apps.system.serializers import OrganizationSimpleSerializer, UserSimpleSerializer


class EquipmentSerializer(ModelSerializer):
    belong_dept_ = OrganizationSimpleSerializer(source='belong_dept', read_only=True)
    keeper_ = UserSimpleSerializer(source='keeper', read_only=True)
    class Meta:
        model = Equipment
        fields = '__all__'

    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('belong_dept','keeper')
        return queryset

class EquipmentSimpleSerializer(ModelSerializer):
    class Meta:
        model = Equipment
        fields = ['id', 'number', 'name']