jeffknupp / jeffknupp/sprime

how to convert date and datetime with as_dict?

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
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.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.