[Answered ]-Add warning message in django-rest-framework

2👍

You can add a field warning_message to the serializer as follows –

class LocationTrackSerializer(serializers.ModelSerializer):
    # rest of the code

    def get_warning_message(self, obj):
        warning_msg = ''

        # logic for checking overlapping dates
        # create a method `are_dates_overlapping` which takes
        # start and end date of the current obj and checks with all
        # others in queryset.
        overlap = are_dates_overlapping(obj.start, obj.end)
        if overlap:
            warning_msg = 'overlaps'

        return warning_msg

    warning_message = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Eventdetail
        fields = ('id','employee','location','location_color','start','end', 'warning_message')

Ref: Serializer Method Field in DRF

👤Sachin

Leave a comment