[Answered ]-Django rest framework, returning item status booked or free based on current date/time

1đź‘Ť

âś…

You can use a SerializerMethodField to add another field that returns a value from a calculation you need:

import django.utils import timezone


class BookingSerializer(serializers.ModelSerializer):
    status = serializers.SerializerMethodField()
    class Meta:
        model = Booking
        fields = (
            'item', 'check_in', 'check_out', 'user', 'status'
        )

    def get_status(self, obj):
        return 'Booked' if obj.check_in <= timezone.now() < obj.check_out else 'Free'
👤Brian Destura

Leave a comment