I can't convert flask_sqlalchemy's ORM to JSON and save to a list

Viewed 27

I'm writing a flask demo, I tried convert ORM to json and return to front.

I create a convert to json function for my ORM, and them should save to my list, but it not work, the exception say my list not have anything, that's why?

this is my ORM:

    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
        }

this is my code:

@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')

this is Exception:

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)
1 Answers

I finisih it.

I shouldn't use append(), that's no make sense or else other thing, I change the way

I still keep my ORM's to_json() function, so I always quick convert to json Stirng, I just need in list for-each.

@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')
Related