0
votes

The problem I have is that I don't seem to be able to filter my data based on timestamp, i.e. both date and hour.

My model looks as follows:

# 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'

My view:

class HourlyTickList(ListAPIView):
    serializer_class = HourlyTickSerializer
    def get(self, request):
        start = request.GET.get('start', None)
        end = request.GET.get('end', None)
        tz = pytz.timezone("Europe/Paris")

        start_dt = datetime.datetime.fromtimestamp(int(start) / 1000, tz)
        end_dt = datetime.datetime.fromtimestamp(int(end) / 1000, tz)

        qs = HourlyTick.objects.filter(timestamp__range = (start_dt, end_dt))
        rawData = serializers.serialize('python', qs)
        fields = [d['fields'] for d in rawData]
        fieldsJson = json.dumps(fields, indent=4, sort_keys=True, default=str)

        return HttpResponse(fieldsJson, content_type='application/json')

The message I receive is:

RuntimeWarning: DateTimeField HourlyTick.timestamp received a naive datetime (2017-01-15 06:00:00) while time zone support is active.
RuntimeWarning)

However, when I use make_aware to fix this error, I get the error:

ValueError: Not naive datetime (tzinfo is already set)

My database contains data that looks like this:

2017-01-06T12:00:00.000Z

For some reason, the first option returns results, but it totally ignores the time.

How do I fix this?

1
to your naive datetime do import pytz mynaicedatetime.replace(tzinfo=pytz.UTC)Yugandhar Chaudhari
@YugandharChaudhari This doesn't solve my problem.html_programmer

1 Answers

0
votes

The problem was because Python couldn't interpret the format I had stored in the database. Two solutions possible:

  • Writing a raw query with string transformation in Django
  • Storing the datetime fields in a different format

I went with option 2 since it was an already automated script for retrieving the data and now it works fine.