how to convert date and datetime with as_dict?
- Dominant language
- Python
- Stars
- 9
- Forks
- 1
- PR merge metrics
- No merged PRs in 30d
Description
This is really a question, not an issue, but I am just wondering how you handle "jsonifying" date and datetimes. I've mutated your as_dict function to look something like this, but I think it is not very elegant:
```
class JSONModel(object):
def as_dict(self):
"""Convert date/datetime to string inorder to jsonify"""
result = {}
for column in self.__table__.columns:
try:
if column.type.python_type in [type(date(2000,1,1)),type(datetime(2000,1,1))]:
if getattr(self, column.name) is not None:
result[column.name] = str(getattr(self, column.name))
else: #don't convert None to string
result[column.name] = getattr(self, column.name)
else:
result[column.name] = getattr(self, column.name)
except Exception, e:
#print("JSONModel error ", column.type, e)
result[column.name] = getattr(self, column.name)
return result
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.