encode / orm

An async ORM. 🗃
https://www.encode.io/orm
BSD 3-Clause "New" or "Revised" License
1.78k stars 98 forks source link

How to convert typesystem to JSON? #152

Open TC-THREE opened 2 years ago

TC-THREE commented 2 years ago

i need convert typesystem to json, but there is no way to find to_json

iused:

class BaseDatabase(orm.Model):
    fields = {}

    def __iter__(self):
        return self.__next__()

    def __next__(self):
        for k in self.fields.keys():
            if hasattr(self, k):
                yield (k, getattr(self, k))
class Json(js.JSONEncoder):

    def default(self, o):
        if isinstance(o, BaseDatabase):
            tmp = dict(o)
            if len(tmp.keys()) == 1 and "id" in tmp.keys():   # ForeignKey
                return tmp['id']
            return tmp
        return super().default(o)
class Mall(BaseDatabase)