1

I am writing/learning my first Django REST API. The database I am working on has all times are stored as an integers representing the seconds since Jan 01 1970 - aka Unix/Epoch time.

Right now I simply have two function that convert between this integer and a normal timestamp. So user input through parameters is converted to integers, and JSON responses are converted to timestamps.

I was wondering if there was a way to simply have Django "know" this conversion. So in my model:

class User(models.Model):
    createDate = models.IntegerField(blank=True, null=True, db_column='createdate')

I could change to models.DateTimeField and createDate would be a time stamp. Correspondingly if I call:

data = Subgroup.objects.filter(createDate_gt='2016-30-04')

In my view Django knows to convert this to an integer and filter by that, rather than the string interpretation.

I know it's a pretty pedantic request but I've been impressed with Django so far and was curious :)

erik-sn
  • 2,590
  • 1
  • 20
  • 37

0 Answers0