samedi 19 septembre 2015

Datetime differences in Django using SQLLite

I'm creating a Django app that needs to perform date differences. Given a model with a start_date and end_date, both DateFields, on Postgres this works as a command like:

model.objects.annotate(difference=F(end_date)-F(start_date))

will work perfectly fine. However, on an SQLite backend this doesn't work as well.

Rather than giving a timedelta (or similar), it returns a string, which is approximately the difference in years. However, SQLite has a command julianday() which converts the date to a "julian day" which can at least be used to get a difference in days.

For example, on the dbshell, this will give the right number of days difference:

SELECT julianday(end_date) - julianday(start_date) FROM 'appname'.'model'; 

Is there a way I can:

  1. Check the database backend - eg. is SQLite or not?

  2. and, then if it is SQLite wrap it in the julianday function?

Aucun commentaire:

Enregistrer un commentaire