-
Notifications
You must be signed in to change notification settings - Fork 68
/
serialize.py
62 lines (43 loc) · 1.54 KB
/
serialize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import print_function
import sqlalchemy as sa
from sqlalchemy.orm import scoped_session, sessionmaker, DeclarativeBase
from sqlalchemy_mixins import SerializeMixin
class Base(DeclarativeBase):
__abstract__ = True
engine = sa.create_engine('sqlite:///:memory:')
session = scoped_session(sessionmaker(bind=engine))
class BaseModel(Base, SerializeMixin):
__abstract__ = True
pass
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')
class Post(BaseModel):
__tablename__ = 'post'
id = sa.Column(sa.Integer, primary_key=True)
body = sa.Column(sa.String)
user_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'))
Base.metadata.create_all(engine)
bob = User(name='Bob' , password = "pass123")
session.add(bob)
session.flush()
post1 = Post(body='Post 1', user=bob)
session.add(post1)
session.flush()
post2 = Post(body='Post 2', user=bob)
session.add(post2)
session.flush()
# {'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 , 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' , 'password' : 'pass123'}}
print(post2.to_dict(nested=True))