base.py 893 Bytes
# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : base.py
# @Time       : 2020/11/18 下午12:47
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------
import datetime

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()


class BaseModel():
    """."""

    def __init__(self):
        pass

    def to_dict(self, allow_field=None):
        all_field = [r.name for r in self.__table__.columns]
        if allow_field:
            allow_field = set(allow_field) & set(allow_field)
        else:
            allow_field = all_field
        return {c: int(getattr(self, c).timestamp()) if isinstance(getattr(self, c), datetime.datetime) else getattr(self, c) for c in allow_field}