![]() ![]() # Note that when the object type is incompatible, sqlite3 will shout. # This is how I ended up with isoformatted dates in my db, by (incorrectly) manipulating an isoformatted string at some point (while I thought I had a datetime object) and Peewee / sqlite3 not throwing any error at me. This format does not match any of the known formats for DateTimeField, so we hit which is completely silent and the value is inserted as is in the database. # Create a person with an isoformatted string. # This is therefore **the same behavior** as before, as expected uncle_sam = Person( String format matches one of so we hit and it is promoted to datetime object. # Create a person with a string representing a date. # Create a person with a datetime object as birthday, this is the most typical use case uncle_bob = Person( name = 'Bob', birthday = datetime( 1960, 1, 15, 1, 0, 0)) update your data to use the python format (probably undesirable, but mentioning it as a possibility)Īll that said, at the end of the day this is not really a peewee issue - this is a consequence of the behavior of the python sqlite driver and sqlite's own lack of a dedicated datetime type.įrom datetime import datetime from peewee import * db = SqliteDatabase( 'people.db')ĭatabase = db # This model uses the "people.db" database.write a custom DateTimeField subclass that implements a db_value() method that returns the datetime in isoformat.register a different adapter for datetime.datetime that converts them to string in iso format - this may be the easiest.This is consistent and supports lexicographic ordering, but if you've stored your datetimes in ISO format in Sqlite, then you may have some work to do to get them working properly with python's sqlite3 module. It is actually the sqlite driver that is handling the conversion for sqlite: ( import sqlite3 sqlite3.adapters). Additionally, Peewee does not apply any conversion at all to datetime objects - it hands them as-is to the db driver. It is up to you, the developer, to ensure that your datetimes are stored in a way that supports correct lexicographic ordering. Sqlite does not have a dedicated date/datetime type.
0 Comments
Leave a Reply. |