Borrowing-TrackerBackend/equipment_tracker/breakages/serializers.py

29 lines
1.1 KiB
Python
Raw Normal View History

2023-12-09 00:38:29 +08:00
from rest_framework import serializers
from accounts.models import CustomUser
from equipments.models import EquipmentInstance
from equipments.serializers import EquipmentInstanceSerializer
from .models import Transaction
from accounts.models import CustomUser
from config.settings import DEBUG
from .models import BreakageReport
class BreakageReportSerializer(serializers.HyperlinkedModelSerializer):
transaction = serializers.SlugRelatedField(
many=False, slug_field='id', queryset=Transaction.objects.all(), required=True)
equipments = serializers.SlugRelatedField(
many=True, slug_field='id', queryset=EquipmentInstance.objects.all())
class Meta:
model = BreakageReport
2023-12-09 00:59:17 +08:00
fields = ['id', 'transaction', 'equipments', 'resolved', 'timestamp']
read_only_fields = ['id', 'transaction',
'equipments', 'resolved', 'timestamp']
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['equipments'] = [
eq.__str__() for eq in instance.equipments.all()]
return representation