0

I'm trying to return an object attribute from a specific route. It would be a datetime return.

Below is my class definition :

from datetime import datetime
from app import db

class CorrectiveAction(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    foo_text = db.Column(db.String(500))
    realized_date = db.Column(db.DateTime, index=True)

And my routing :

from datetime import datetime
from app.models.correctiveaction import CorrectiveAction
from app import app, db

@app.route('/action/corrective/realize/corrective_id', method=['GET'], )
@login_required
def realize_corrective(corrective_id):
correctiveAction = CorrectiveAction.query.filter_by(id=corrective_id).first()
if not correctiveAction.realized_date:
    correctiveAction.realized_date = datetime.utcnow()
    db.session.commit()
return correctiveAction.realized_date

Whenever I try to access this URL, my date is set up properly, but I can't get any value in return. This is the stack trace I get :

[2018-05-23 17:51:53,621] ERROR in app: Exception on /action/corrective/realize/1 [GET]
Traceback (most recent call last):
  File "C:\Users\FRNNUFTA\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\FRNNUFTA\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1615, in full_dispatch_request
    return self.finalize_request(rv)
  File "C:\Users\FRNNUFTA\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1630, in finalize_request
    response = self.make_response(rv)
  File "C:\Users\FRNNUFTA\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1740, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "C:\Users\FRNNUFTA\AppData\Local\Programs\Python\Python36-32\lib\site-packages\werkzeug\wrappers.py", line 921, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "C:\Users\FRNNUFTA\AppData\Local\Programs\Python\Python36-32\lib\site-packages\werkzeug\test.py", line 923, in run_wsgi_app
    app_rv = app(environ, start_response)
TypeError: 'datetime.datetime' object is not callable

I think this error isn't related to datetime specificaly, as I get the same type when I try to return another value from my object.

Thanks,

Fratass
  • 13
  • 1
  • 4

1 Answers1

1

Essentially, the error you are seeing is because Flask doesn't know how to turn a datetime.datetime object into data it can send as part of an HTTP response.

This tutorial is a good primer on flask responses.

You probably need to decide the best string representation for your datetime object and use that as the return value.

From the documentation itself:

The logic that Flask applies to converting return values into response objects is as follows:

  1. If a response object of the correct type is returned it’s directly returned from the view.
  2. If it’s a string, a response object is created with that data and the default parameters.
  3. If a tuple is returned the items in the tuple can provide extra information. Such tuples have to be in the form (response, status, headers) or (response, headers) where at least one item has to be in the tuple. The status value will override the status code and headers can be a list or dictionary of additional header values.
  4. If none of that works, Flask will assume the return value is a valid WSGI application and convert that into a response object.

Since your datetime object does not meet 1, 2 or 3, Flask tries to treat it as 4 and call it as a WSGI application. That's why the precise error you get is object is not callable.

jfowkes
  • 1,495
  • 9
  • 17