diff --git a/examples/serialize.py b/examples/serialize.py index 5e6ce0a..b2008cb 100644 --- a/examples/serialize.py +++ b/examples/serialize.py @@ -18,8 +18,8 @@ class BaseModel(Base, SerializeMixin): class User(BaseModel): __tablename__ = 'user' - id = sa.Column(sa.Integer, primary_key=True) + password = sa.Column(sa.String) name = sa.Column(sa.String) posts = sa.orm.relationship('Post', backref='user') @@ -34,7 +34,7 @@ class Post(BaseModel): Base.metadata.create_all(engine) -bob = User(name='Bob') +bob = User(name='Bob' , password = "pass123") session.add(bob) session.flush() @@ -46,17 +46,17 @@ class Post(BaseModel): session.add(post2) session.flush() -# {'id': 1, 'name': 'Bob'} +# {'id': 1, 'name': 'Bob' , 'password' : 'pass123'} print(bob.to_dict()) # {'id': 1, # 'name': 'Bob', # 'posts': [{'body': 'Post 1', 'id': 1, 'user_id': 1}, # {'body': 'Post 2', 'id': 2, 'user_id': 1}]} -print(bob.to_dict(nested=True)) +print(bob.to_dict(nested=True , exclude = ['password'])) # {'id': 1, 'body': 'Post 1', 'user_id': 1} print(post1.to_dict()) -# {'id': 2, 'body': 'Post 2', 'user_id': 1, 'user': {'id': 1, 'name': 'Bob'}} +# {'id': 2, 'body': 'Post 2', 'user_id': 1, 'user': {'id': 1, 'name': 'Bob' , 'password' : 'pass123'}} print(post2.to_dict(nested=True)) diff --git a/sqlalchemy_mixins/serialize.py b/sqlalchemy_mixins/serialize.py index f64bf1a..7df8b6c 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): + 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,7 +17,13 @@ def to_dict(self, nested=False, hybrid_attributes=False): :return: dict """ result = dict() - for key in self.columns: + + 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) if hybrid_attributes: diff --git a/sqlalchemy_mixins/serialize.pyi b/sqlalchemy_mixins/serialize.pyi index ead75b3..df9d3a6 100644 --- a/sqlalchemy_mixins/serialize.pyi +++ b/sqlalchemy_mixins/serialize.pyi @@ -1,6 +1,6 @@ from sqlalchemy_mixins.inspection import InspectionMixin - +from typing import Optional , List class SerializeMixin(InspectionMixin): - def to_dict(self, nested: bool = False, hybrid_attributes: bool = False) -> dict: ... \ No newline at end of file + def to_dict(self, nested: bool = False, hybrid_attributes: bool = False, exclude: Optional[List[str]] = None) -> dict: ... diff --git a/sqlalchemy_mixins/tests/test_serialize.py b/sqlalchemy_mixins/tests/test_serialize.py index 2861781..7733090 100644 --- a/sqlalchemy_mixins/tests/test_serialize.py +++ b/sqlalchemy_mixins/tests/test_serialize.py @@ -22,6 +22,7 @@ class User(BaseModel): id = sa.Column(sa.Integer, primary_key=True) name = sa.Column(sa.String) posts = sa.orm.relationship('Post') + password = sa.Column(sa.String) @hybrid_property def posts_count(self): @@ -62,11 +63,11 @@ def setUp(self): self.session = Session(self.engine) Base.metadata.create_all(self.engine) - user_1 = User(name='Bill u1', id=1) + user_1 = User(name='Bill u1', id=1 , password = 'pass1') self.session.add(user_1) self.session.commit() - user_2 = User(name='Alex u2', id=2) + user_2 = User(name='Alex u2', id=2 , password = 'pass2') self.session.add(user_2) self.session.commit() @@ -93,7 +94,9 @@ def tearDown(self): Base.metadata.drop_all(self.engine) def test_serialize_single(self): - result = self.session.query(User).first().to_dict() + result = self.session.query(User)\ + .first()\ + .to_dict(exclude= ['password']) expected = { 'id': 1, 'name': 'Bill u1' @@ -101,7 +104,7 @@ def test_serialize_single(self): self.assertDictEqual(result, expected) def test_serialize_list(self): - result = [user.to_dict() for user in self.session.query(User).all()] + result = [user.to_dict(exclude = ['password']) for user in self.session.query(User).all()] expected = [ { 'id': 1, @@ -124,7 +127,8 @@ def test_serialize_nested(self): 'user_id': 1, 'user': { 'id': 1, - 'name': 'Bill u1' + 'name': 'Bill u1' , + 'password' : 'pass1' }, 'comments': [ { @@ -139,7 +143,9 @@ def test_serialize_nested(self): self.assertDictEqual(result, expected) def test_serialize_single__with_hybrid(self): - result = self.session.query(User).first().to_dict(hybrid_attributes=True) + result = self.session.query(User)\ + .first()\ + .to_dict(hybrid_attributes=True , exclude = ['password']) expected = { 'id': 1, 'name': 'Bill u1', @@ -157,7 +163,8 @@ def test_serialize_nested__with_hybrid(self): 'user': { 'id': 1, 'name': 'Bill u1', - 'posts_count': 1 + 'posts_count': 1 , + 'password' : 'pass1' }, 'comments': [ {