Skip to content

Commit

Permalink
Merge pull request #106 from ral-facilities/105_problem_with_returnin…
Browse files Browse the repository at this point in the history
…g_decimals

Problem with returning decimals
  • Loading branch information
keiranjprice101 authored Oct 24, 2019
2 parents 4f15e86 + b78d251 commit 1c99cac
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion 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 @@ -50,9 +52,22 @@ def to_dict(self):
dictionary = {}
for column in self.__table__.columns:
attribute = getattr(self, column.name)
dictionary[column.name] = str(attribute) if isinstance(attribute, datetime) else attribute
dictionary[column.name] = self._make_serializable(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 1c99cac

Please sign in to comment.