Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
2 views
by (19.9k points)

I want to apply a range filter on a timestamp field, based on query parameters start and end.

My view looks like this:

from rest_framework.generics import ListAPIView

from rest_framework.generics import RetrieveAPIView

from .models import HourlyTick

from .serializer import HourlyTickSerializer

# Create your views here.

class HourlyTickList(ListAPIView):

    def get(self, request):

        start = request.GET.get('start', None)

        end = request.GET.get('end', None)

        return HourlyTick.objects.filter(timestamp__range(start, end))

class HourlyTickDetail(RetrieveAPIView):

    queryset = HourlyTick.objects.all()

    serializer_class = HourlyTickSerializer

Model:

from django.db import models

# Create your models here.

class HourlyTick(models.Model):

    id = models.IntegerField(primary_key=True)

    timestamp = models.DateTimeField(blank=True, null=True)

    symbol = models.TextField(blank=True, null=True)

    open = models.IntegerField(blank=True, null=True)

    high = models.IntegerField(blank=True, null=True)

    low = models.IntegerField(blank=True, null=True)

    close = models.IntegerField(blank=True, null=True)

    trades = models.IntegerField(blank=True, null=True)

    volume = models.IntegerField(blank=True, null=True)

    vwap = models.FloatField(blank=True, null=True)

    class Meta:

        managed = False

        db_table = 'xbtusd_hourly'

urls.py:

from django.urls import path, re_path

from . import views

urlpatterns = [

    re_path('hourlyticks', views.HourlyTickList.as_view())

]

The error I receive is:

name 'timestamp__range' is not defined

What is the problem here?

1 Answer

0 votes
by (25.1k points)

You're missing an equal sign. It should be:

return HourlyTick.objects.filter(timestamp__range=(start, end))

You will also need to convert your datetime strings to datetime objects.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 8, 2021 in Web Technology by Rekha (2.2k points)

Browse Categories

...