I have an api with an end day field (ad end time), in the model, this is a simple datetime field:
end_date = DateTimeField(null=True, blank=True)
in my view there are several types of filtering for ads, but I need some checkbox that will compare the current date and time with the one written in this field, based on which only relevant ads will be displayed Is there something similar in the DRF, like this(DjangoFilterBackend):
class AdAPI(generics.ListAPIView):
queryset = Ad.objects.all()
serializer_class = AdSerializers
filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
filterset_class = AdFilterSet
I will be grateful for the hint
You can use __lt
, __lte
, __gt
, and __gte
suffixes in a filter query to obtain the set of objects that fulfill those parameters.
See here: Django queryset field lookups
For example:
now = datetime.datetime.now()
ads_later_than_now = Ad.objects.filter(end_date__gt=now)