Skip to content

Commit

Permalink
#43: Allow many to many includes
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Aug 20, 2019
1 parent 60c668d commit 107e7c6
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions common/models/db_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from sqlalchemy import Index, Column, BigInteger, String, DateTime, ForeignKey, Integer, Float, FetchedValue
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm.collections import InstrumentedList

from common.exceptions import BadFilterError

Expand Down Expand Up @@ -34,10 +35,18 @@ def to_nested_dict(self, includes):
for include in includes:
if type(include) is str:
related_entity = self.get_related_entity(include)
if not isinstance(related_entity, InstrumentedList):
dictionary[related_entity.__tablename__] = related_entity.to_dict()
else:
for entity in related_entity:
dictionary[f"{entity.__tablename__} {entity.ID}"] = entity.to_dict()
elif type(include) is dict:
related_entity = self.get_related_entity(list(include)[0])
if not isinstance(related_entity, InstrumentedList):
dictionary[related_entity.__tablename__] = related_entity.to_nested_dict(include[list(include)[0]])
else:
for entity in related_entity:
dictionary[f"{entity.__tablename__} {entity.ID}"] = entity.to_nested_dict(include[list(include)[0]])
except TypeError:
raise BadFilterError(f" Bad include relations provided: {includes}")
return dictionary
Expand Down

0 comments on commit 107e7c6

Please sign in to comment.