Reputation: 7702
We have an endpoint that lets you search for events between two dates.
/events?start_time=X&end_time=X
Events are a model with:
Should we be doing validation on the start_time
and end_time
parameters in the View, Serializer, or Model?
We want to make sure the start_time
parameter is included, end_time
is optional, and ensure that both are well formatted dates.
Is this custom logic in the view or is there a set of helpers that DRF (or Django) provides to perform this validation?
Upvotes: 0
Views: 431
Reputation: 1966
This is filtering, so it should be done on the queryset in the view. Validation of that data should also be done there I guess. You may use a dedicated serializer to validate that data (in get_queryset
for example).
However, I would suggest using django-filter which will take care if the validation for you.
Maybe a filterset like this:
from django_filters import rest_framework as filters
class EventFilterset(filters.FilterSet)
start_time=filters.DateFilter(name='event_date', lookup_expr='gte')
end_time=filters.DateFilter(name='event_date', lookup_expr='lte')
class Meta:
model=Events
fields=['start_time', 'end_time']
I'm not sure if you need the class Meta
part.
Full documentation here.
Upvotes: 1