Converting a datetime to a date in database


(Sergi Almacellas Abellana) #1

Continuing the discussion from Search inside Dict:

The Cast function from python-sql can be used to convert from datetime (or timestampt) to date:

>>> from sql import Table, Cast
>>> t = Table('a')
>>> tuple(t.select(Cast(t.date, 'DATE')))
('SELECT CAST("a"."date" AS DATE) FROM "a" AS "a"', ())

In tryton you can use sql_type function to determine the database type used to represent a field.

So inside tryton you can use:

Model = Pool().get('model.name')
table = Model.__table__()
table.select(Cast(table.datetime, Model.date.sql_type().base)              

To select the value of the datetime field converted to the date field sql type.

Hope it helps