From db8e05543aab6fedfe1105440c69cbbd3266bf8b Mon Sep 17 00:00:00 2001 From: Jeancarlos De la Cruz Criollo Date: Tue, 3 May 2022 16:57:50 -0500 Subject: [PATCH] Add exclude to relations --- sqlalchemy_mixins/serialize.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/sqlalchemy_mixins/serialize.py b/sqlalchemy_mixins/serialize.py index 402ccc0..c52c03f 100644 --- a/sqlalchemy_mixins/serialize.py +++ b/sqlalchemy_mixins/serialize.py @@ -8,7 +8,7 @@ class SerializeMixin(InspectionMixin): __abstract__ = True - def to_dict(self,nested = False, hybrid_attributes = False, exclude = None): + def to_dict(self, nested=False, hybrid_attributes=False, exclude=None): """Return dict object with model's data. :param nested: flag to return nested relationships' data if true @@ -17,26 +17,25 @@ def to_dict(self,nested = False, hybrid_attributes = False, exclude = None): :type: bool :return: dict """ - result = dict() - if exclude is None: - view_cols = self.columns - else : - view_cols = filter(lambda e: e not in exclude, self.columns) - - for key in view_cols : - result[key] = getattr(self, key) + view_cols = self.columns + relations = self.relations + else: + view_cols = filter(lambda e: e not in exclude, self.columns) + relations = filter(lambda r: r not in exclude, self.relations) + result = {key: getattr(self, key) for key in view_cols} if hybrid_attributes: for key in self.hybrid_properties: result[key] = getattr(self, key) if nested: - for key in self.relations: + for key in relations: obj = getattr(self, key) if isinstance(obj, SerializeMixin): - result[key] = obj.to_dict(hybrid_attributes=hybrid_attributes) + result[key] = obj.to_dict( + hybrid_attributes=hybrid_attributes) elif isinstance(obj, Iterable): result[key] = [ o.to_dict(hybrid_attributes=hybrid_attributes) for o in obj