title: Right way to update a date attribute in a database?
body: <p>I am looking for a way to update data input (date format) inside a database.</p>
  <p>Client app --&gt; Flask --&gt; Database</p>
  <p>I am unsure about the best way for processing that needs to be done within Flask.</p>
  <p>Data from the client app comes in JSON and may look like this:</p>
  <pre><code>data = [{&quot;id&quot;: 1, &quot;date&quot;: &quot;2022-01-01&quot;}, {&quot;id&quot;: 2, &quot;date&quot;: null}]
  </code></pre>
  <p>Note that date can also be null.</p>
  <p>Currently I am processing the date input in the following way. Is there a better way to do this instead of manually checking if the provided data is empty?</p>
  <pre><code>import datetime 
  
  @app.route('/update_date', methods=['POST'])
  def update_date():
      data = request.json['data']
  
      for item in data:
          # query db object
          example = db.session.query(Example).filter_by(id=data['id']).first()
          
          # checking if input exists --&gt; looking for a better way to do this
          if item['date'] == '' or item['date'] is None:
              example.date = None
          else:
              # convert string date to actual date object
              example.date = datetime.datetime.strptime(data['date'], '%d.%m.%Y')
  
          db.session.commit()
  </code></pre>
  <p>One drawback of this method is that the data input needs to be in the format specified in the strptime function. If the data comes in a different format it will raise an error. But there has to be a better way than to just add anotehr elif statement, right?</p>
  <p>database model:</p>
  <pre><code>from flask_sqlalchemy import SQLAlchemy
  from sqlalchemy import Date
  
  db = SQLAlchemy()
  
  class Example(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      date = db.Column(db.Date)
  </code></pre>