From 2561d821c9485ff2c6fb65bad86f1ad8498c3a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E4=BA=8C=E7=8C=9B?= Date: Mon, 22 Jul 2019 10:41:30 +0800 Subject: [PATCH] =?UTF-8?q?U=20-=20=E5=A2=9E=E5=BC=BAto=5Fjson=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/libs/model.py | 11 ++++++++--- spug_api/libs/tools.py | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/spug_api/libs/model.py b/spug_api/libs/model.py index af8edc3..155618c 100644 --- a/spug_api/libs/model.py +++ b/spug_api/libs/model.py @@ -47,7 +47,12 @@ class ModelMixin(object): record = cls(**kwargs).save() return record - def to_json(self): - if hasattr(self, '__table__'): + def to_json(self, excludes=None, selects=None): + if not hasattr(self, '__table__'): + raise AssertionError('<%r> does not have attribute for __table__' % self) + elif selects: + return {i: getattr(self, i) for i in selects} + elif excludes: + return {i.name: getattr(self, i.name) for i in self.__table__.columns if i.name not in excludes} + else: return {i.name: getattr(self, i.name) for i in self.__table__.columns} - raise AssertionError('<%r> does not have attribute for __table__' % self) diff --git a/spug_api/libs/tools.py b/spug_api/libs/tools.py index 860c0b4..0b7d9e1 100644 --- a/spug_api/libs/tools.py +++ b/spug_api/libs/tools.py @@ -70,7 +70,7 @@ class Argument(object): :param bool required: is required """ - def __init__(self, name, default=None, required=True, type=str, filter=None, help=None, nullable=False): + def __init__(self, name, default=None, required=True, type=None, filter=None, help=None, nullable=False): self.name = name self.default = default self.type = type