Closed wil93 closed 9 years ago
you need to provide a custom json encoder:
from eve.io.base import BaseJSONEncoder
from eve_sqlalchemy import SQL as SQLAlchemy
from datetime import timedelta
class SQLAJSONEncoder(BaseJSONEncoder):
def default(self, obj):
if isinstance(obj, timedelta):
return int(obj)
return super(SQLAJSONEncoder, self).default(obj)
class CustomEve(SQLAlchemy):
json_encoder_class = SQLAJSONEncoder
app = Eve(data=CustomEve)
Thank you, however I keep getting the same error... I modified the "simple example" from eve-sqlalchemy website in order to demonstrate the error: this is the version that does not include the custom JSON encoder, and this is the version that includes it. Both versions give the aforementioned TypeError.
I will check it
Hi, any news about this? :smile:
I was thinking that this is probably related to nicolaiarocci/eve#267
It shouldn't be related - eve-sqlalchemy depends on eve>=0.5
There might be an issue with eve.utils.document_etag() function. Debugging the execution of your script with pdb, I can see eve is trying to generate the etag by encoding attributes with a simple json.dumps() instead of json_encoder_class like in eve.render.render_json().
:confused: After looking at imports in eve/utils.py, the dumps() function comes from bson
and not from json
so I'm not sure the custom json encoder class can be used here.
Now that you mention it, I put a __import__("pdb").set_trace()
in the default(self, obj)
function to see if it was called, and that point was never reached. Now this bson
thing could explain that.
@wil93 you should also change int(obj)
by obj.total_seconds()
since int()
can't take a timedelta
object directly.
Right, thanks :smile:
This issue should be fixed in Eve 0.5.4 (cf. eve@72036276f2204d59e54a8a9fda5a6cc98f21143a).
Confirmed - https://github.com/nicolaiarocci/eve/commit/72036276f2204d59e54a8a9fda5a6cc98f21143a fix the bug
Hi, I just started using eve-sqlalchemy but I get an error when trying to access an object which has a timedelta field like this:
The error I get is: