适用于Datatables.net的带有Flask-Restless的Json数组

时间:2015-10-29 15:10:25

标签: arrays json serialization datatables flask-restful

我想使用Datatables.net,它需要和json数组一样,如下所示

{
  "data": [
    [
      "9",
      "1",
      "sala",
      "zeus",
      "lights",
      "3",
      "7",
      "tcp",
    ]
}

但是在Flask-Restfull上,我能从中得到的最近的是

{
  "objects": [
    {
      "channel": 9, 
      "id": 1, 
      "location": "sala", 
      "name": "zeus", 
      "purpose": "lights", 
      "role_id": 3, 
      "role_permissions": 7, 
      "type": "tcp"
    }
}

使用此设置:

manager.create_api(Device,
                   results_per_page=1000,
                   primary_key='id',
                   exclude_columns=['user_devices','role','num_results'],
                   app=app,
                   # exclude_columns=['role_id'],
                   preprocessors=dict(GET_SINGLE=[auth_func],
                                      GET_MANY=[auth_func]),
                   postprocessors={
                       'GET_MANY': [api_post_get_many]
                   }
)


def api_post_get_many(result=None, **kw):
    for key in result.keys():
        if key != 'objects':
            del result[key]

和这个模型:

class Device(db.Model):
    __tablename__ = 'devices'

    __table_args__ = (db.UniqueConstraint('id', 'name', 'purpose', 'channel', 'type','location'),
                      db.ForeignKeyConstraint(
                                        ['role_id', 'role_permissions'],
                                        ['roles.id', 'roles.permissions']
                            )
                      )
    id = db.Column(db.Integer, primary_key=True)
    purpose = db.Column( db.String(64))
    type = db.Column( db.String(64))
    name = db.Column(db.String(64))
    location = db.Column(db.String(64))

    channel = db.Column( db.Integer)
    role_id = db.Column(db.Integer)
    role_permissions = db.Column(db.Integer)
    role = db.relationship('Role', backref=db.backref('devices'))

有没有办法让FlaskRESTLESS返回一个json数组而不是一个json对象?或者我应该尝试使用python序列化数据?

0 个答案:

没有答案