加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Flask-Restless order_by相关模型

发布时间:2020-12-20 13:45:08 所属栏目:Python 来源:网络整理
导读:我有这两个模型(使用Flask-SQLAlchemy定义): class BankAccount(db.Model): id = db.Column(db.Integer,primary_key=True) customer_id = db.Column(db.Integer,db.ForeignKey('customer.id')) created_at = db.Column(db.DateTime)class Customer(db.Model
我有这两个模型(使用Flask-SQLAlchemy定义):

class BankAccount(db.Model):
    id            = db.Column(db.Integer,primary_key=True)
    customer_id   = db.Column(db.Integer,db.ForeignKey('customer.id'))
    created_at    = db.Column(db.DateTime)

class Customer(db.Model):
    id            = db.Column(db.Integer,primary_key=True)
    name          = db.Column(db.String(128),index=True)

我使用以下查询参数为BankAccount使用Flask-Restless API端点没有问题:

http://mywebsite.com/api/bank_account?q={"order_by":[{"field":"created_at","direction":"asc"}]}

但是,我想按客户名称订购这些结果(银行账户) – 而客户是相关模型,因此BankAccount模型中没有customer_name这样的字段.

有没有简单的方法让这个订购工作?在一些已知的Python ORM中,这可以通过使用“双下划线”来实现,例如:

http://mywebsite.com/api/bank_account?q={"order_by":[{"field":"customer__name","direction":"asc"}]}

我也尝试过:

{"field":"Customer","direction":"asc"}
{"field":"customer","direction":"asc"}
{"field":"customer.name","direction":"asc"}

对于所有这些尝试,我得到了这样的回应:

{
  "message" : "Unable to construct query"
}

有关如何进行此查询的任何想法?或者使用Flask-Restless这是不可能的?谢谢!

解决方法

你最好的选择可能是使用后处理器:

https://flask-restless.readthedocs.org/en/latest/customizing.html?highlight=order#request-preprocessors-and-postprocessors

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读