首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我无法将烧瓶_sqlalchemy的ORM转换为JSON并保存到列表

我无法将烧瓶_sqlalchemy的ORM转换为JSON并保存到列表
EN

Stack Overflow用户
提问于 2022-09-15 01:37:03
回答 1查看 29关注 0票数 0

我正在写一个烧瓶演示,我试着把ORM转换成json,然后返回到前面。

我为我的ORM创建了一个json函数,它们应该保存到我的列表中,但是它不起作用,例外说我的列表没有任何东西,这就是为什么?

这是我的ORM:

代码语言:javascript
复制
    class Metas(db.Model):
    __tablename__ = 'typecho_1metas'
    mid = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.VARCHAR(200), nullable=False)
    slug = db.Column(db.VARCHAR(200), nullable=False)
    type = db.Column(db.VARCHAR(16), nullable=False)
    description = db.Column(db.Text, nullable=False)
    count = db.Column(db.Integer, nullable=False)
    order = db.Column(db.Integer, nullable=False)
    parent = db.Column(db.Integer, nullable=False)

    def to_json(self):
        return {
            'mid': self.mid,
            'name': self.name,
            'slug': self.slug,
            'type': self.type,
            'description': self.description,
            'count': self.count,
            'order': self.order,
            'parent': self.parent
        }
    def to_json_two(self):
        return {
            'name': self.name,
            'count': self.count
        }

这是我的密码:

代码语言:javascript
复制
@app.route('/index_data')
def index_data():
    engine = db.get_engine()
    engine.connect()

    data = Metas.query.filter(Metas.count > 0)
    alltag = [], tags = [], category = []
    for i in data:
        alltag.append(i.to_json_two())
        if i.type == 'tag':
            tags.append(i.to_json_two())
        else:
            category.append(i.to_json_two())


    data_list = {'alltag': alltag, 'tags': tags, 'category': category}

    return Response(json.dumps(data_list), mimetype='application/json')

这是例外:

代码语言:javascript
复制
Traceback (most recent call last):
  File "E:\Work\Py3\pydemo\myDemo\venv\lib\site-packages\flask\app.py", line 2548, in __call__
    return self.wsgi_app(environ, start_response)
  File "E:\Work\Py3\pydemo\myDemo\venv\lib\site-packages\flask\app.py", line 2528, in wsgi_app
    response = self.handle_exception(e)
  File "E:\Work\Py3\pydemo\myDemo\venv\lib\site-packages\flask\app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "E:\Work\Py3\pydemo\myDemo\venv\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "E:\Work\Py3\pydemo\myDemo\venv\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "E:\Work\Py3\pydemo\myDemo\venv\lib\site-packages\flask\app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "E:\Work\Py3\pydemo\myDemo\app.py", line 109, in index_data
    alltag = [], tags = [], category = []
ValueError: not enough values to unpack (expected 2, got 0)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-15 03:11:25

我终于弄清楚了。

我不应该使用append(),这是没有意义的,否则,我改变了方式

我仍然保留着ORM的to_json()函数,所以我总是快速转换为json,我只需要在列表中列出-每一个。

代码语言:javascript
复制
@app.route('/index_data')
def index_data():
    engine = db.get_engine()
    engine.connect()

    # just get all tag
    allmeta = Metas.query.all()
    allmeta_json = [m.name for m in allmeta]

    # get category Metas and convert json
    category = Metas.query.filter(and_(Metas.count > 0), and_(Metas.type == 'category'))
    category_json = [m.to_json_two() for m in category]

    # get tag Metas and convert json
    tag = Metas.query.filter(and_(Metas.count > 0), and_(Metas.type == 'tag'))
    tag_json = [m.to_json_two() for m in tag]

    # add all
    data_list = {'alltag': allmeta_json, 'tags': tag_json, 'category': category_json}

    return Response(json.dumps(data_list), mimetype='application/json')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73724728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档