forked from msooner/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseDO.py
More file actions
39 lines (27 loc) · 948 Bytes
/
BaseDO.py
File metadata and controls
39 lines (27 loc) · 948 Bytes
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
#-*-coding:utf-8-*-
import time
from uuid import uuid4
from sqlalchemy import String, Integer
from sqlalchemy import Column
from sqlalchemy.ext.declarative import declarative_base
class Base(object):
__table_args__ = {
'mysql_charset': 'utf8mb4'
}
@property
def columns(self):
return [c.name for c in self.__table__.columns]
@property
def columnitems(self):
return dict([(c, getattr(self, c)) for c in self.columns])
@property
def dict(self):
return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}
def __repr__(self):
return '{}({})'.format(self.__class__.__name__, self.columnitems)
def tojson(self):
return self.columnitems
class BaseObj(Base):
id = Column(String, primary_key=True, default=lambda: str(uuid4()))
create_time = Column(Integer, default=lambda: int(time.time()))
BaseDO = declarative_base(cls=BaseObj)