I have a model with a timestamp field:
models.py
class Event(models.Model): event_type = models.CharField( max_length=100, choices=EVENT_TYPE_CHOICES, verbose_name=_("Event Type") ) event_model = models.CharField( max_length=100, choices=EVENT_MODEL_CHOICES, verbose_name=_("Event Model") ) timestamp = models.DateTimeField(auto_now=True, verbose_name=_("Timestamp"))
Then I use the Django-rest-framework to create an API endpoint for this class, with a django filter that provides filtering functionality as follows:
from .models import Event from .serializers import EventSerializer from rest_framework import viewsets, filters from rest_framework import renderers from rest_framework_csv import renderers as csv_renderers class EventsView(viewsets.ReadOnlyModelViewSet): """ A read only view that returns all audit events in JSON or CSV. """ queryset = Event.objects.all() renderer_classes = (csv_renderers.CSVRenderer, renderers.JSONRenderer) serializer_class = EventSerializer filter_backends = (filters.DjangoFilterBackend,) filter_fields = ('event_type', 'event_model', 'timestamp')
with the following settings:
REST_FRAMEWORK = { 'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',), }
I can filter event_type and event_model , but I have a problem with filtering by timestamp field. Essentially, I want to make an API call that matches the following:
AuditEvent.objects.filter(timestamp__gte='2016-01-02 00:00+0000')
which, I expect, could do as follows:
response = self.client.get("/api/v1/events/?timestamp=2016-01-02 00:00+0000", **{'HTTP_ACCEPT': 'application/json'})
although it is. How to create an API call that returns all objects with a timestamp greater than or equal to a specific value?