title: Format db.Datetime values automatically in Flask-SQLAlchemy
body: <p>I'm in the process of migrating hosts, and the new hosts uses a newer version of MySQL which is stricter about the format of datetime values it accepts. I know that I can turn this off, but I'd like to find a solution to properly convert these values to the correct format. Preferably one that can be easily swapped out across the many models I need to update (100 or so).</p>
  
  <p>If I can't find a way to update this on the model level, I may have to hunt down the thousands of locations where these values are set.</p>
  
  <p>I've trimmed down an example model here:</p>
  
  <pre><code>class Timesheet(BaseModel, db.Model):
      __tablename__ = "timesheet"
      timesheet_id = db.Column(db.Integer, primary_key=True)
      created = db.Column(db.DateTime, default=datetime.utcnow)
      updated = db.Column(db.DateTime)
      employee_id = db.Column(db.Integer, db.ForeignKey('user.employee_id'))
      description = db.Column(db.String(255))
      hours = db.Column(db.Numeric(9, 2))
      billable = db.Column(db.Boolean, default=False)
      retainer = db.Column(db.Boolean, default=False)
      client_id = db.Column(db.Integer, db.ForeignKey('client.client_id'), default=None)
      project_id = db.Column(db.Integer, db.ForeignKey('project.project_id'), default=None)
      task_id = db.Column(db.Integer, default=None)
      timesheet_date = db.Column(db.DateTime, default=datetime.utcnow)
  </code></pre>
  
  <p>I'm hoping there is a way to modify db.Datetime so that it returns a true Python datetime object when provided with an ISO8601 datetime string ('YYYY-MM-DDT00:00:00.000Z')</p>
  
  <p>Looking for something along the lines of:</p>
  
  <pre><code>class FormattedDateTime(db.DateTime):
      def self.__set__(self, value):
          return dateutil.parser.parse(value)
  </code></pre>
  
  <p>The column definition would then be changed to:</p>
  
  <pre><code>updated = db.Column(FormattedDateTime)
  </code></pre>
  
  <p>...so that it will automatically save with the correct format when SQLAlchemy sends to MySQL.</p>
  
  <p>I've looked at Mixins and searched around for approaches that will accomplish this, but can't seem to find any good solutions. Help is much appreciated.</p>
  
  <p><strong>UPDATED:</strong>
  This is a rough draft of what I've worked out so far... it seems to do well even when filtering/comparing on the field in a query but isn't well tested just yet.</p>
  
  <pre><code>class TFDateTime(TypeDecorator):
      impl = DATETIME
  
      def process_bind_param(self, value, dialect):
          if value is None:
              return None
          print("process_bind_param", value, type(value))
          if type(value) == datetime or type(value) == date:
              return value
          elif type(value) == str:
              return parse(value, ignoretz=True)
          else:
              return None
  
      def process_result_value(self, value, dialect):
          if value is None:
              return None
          print("process_result_value", value, type(value))
          return value
  </code></pre>