[Django]-How to use date__range filter efficiently in django

0👍

Well you are doing __range operator on a CharField, so the range comparison is done on a character basis not a date basis.

You already have a field epochTime = models.IntegerField(null = False), I’ll assume that a UNIX time epoch.

To start soft, you can already do a epochTime__range query that can correctly give results if you pass in an epochTime__range=(timestamp_integer_start, timestamp_integer_end) range. You’ll have to pass an integer that represents the date/time for this.

Otherwise you can add a timestamp = models.DateTimeField() and copy over the values from the epochTime field.

It’s easy to convert from a UNIX timestamp in integer format to a Python datetime object, which you can pass in as your value for the DateTimeField in Django.

Bottom line is, no need to duplicate the date and time fields in your model, use a single field that represents the timestamp. I recommend DateTimeField because your queries will be easier in Django.

👤bakkal

Leave a comment