Skip to content

Commit

Permalink
#105: Create serializer method
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Oct 24, 2019
1 parent 69b7939 commit d294df4
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions common/models/db_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import enum

from datetime import datetime
from decimal import Decimal

from sqlalchemy import Index, Column, BigInteger, String, DateTime, ForeignKey, Integer, Float, FetchedValue, \
TypeDecorator, Boolean
from sqlalchemy.ext.declarative import declarative_base
Expand Down Expand Up @@ -53,6 +55,19 @@ def to_dict(self):
dictionary[column.name] = str(attribute) if isinstance(attribute, datetime) else attribute
return dictionary

def _make_serializable(self, field):
"""
Given a field, convert to a JSON serializable type
:param field: The field to be converted
:return: The converted field
"""
if isinstance(field, datetime):
return str(field)
elif isinstance(field, Decimal):
return float(field)
else:
return field

def to_nested_dict(self, includes):
"""
Given related models return a nested dictionary with the child or parent rows nested.
Expand Down

0 comments on commit d294df4

Please sign in to comment.