如何在Odoo 12中的JSON控制器中获取特定记录 [英] How to get specific record in json controller in Odoo 12

查看:64
本文介绍了如何在Odoo 12中的JSON控制器中获取特定记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题说明了一切.我已经做了一个控制器来搜索特定模型中的所有记录.

My question says it all. I already did a controller to search all records from specific model.

现在,我只想搜索同一模型中的一条记录(按其ID).

Now I want to search just one record (by its id) from the same model.

我对所有记录的控制是这样的:

My controller for all records is this:

@http.route('/get_sales', type='json', auth='user')
    def get_sales(self):
        sales_rec = request.env['sale.order'].search([])
        sales = []
        for rec in sales_rec:
            vals = {
                'id': rec.id,
                'name': rec.name,
                'partner_id': rec.partner_id,
            }
            sales.append(vals)
        data = {'status': 200, 'response': sales, 'message': 'All sales returned'}
        return data

因此,我要添加的内容仅取决于其ID来获得一条记录.

So, what I have to add to get just one record depending on its ID.

谢谢.

推荐答案

首先,像这样更改您的控制器路线:

Firstly, change your controller route like so:

@http.route(['/get_sales', '/get_sales/<int:order_id>'], type='json', auth='user')

通过此操作,您现在可以选择是否使用 order_id 来调用路线.

By doing that you now have the option to call your route with or without an order_id.

第二,像这样更改您的方法:

Secondly, change your method like so:

def get_sales(self, order_id=None):

现在,您可以在方法中添加一些逻辑:

Now you can add some logic into your method:

if order_id:
    domain = [('id', '=', order_id)]
else:
    domain = []
sales_rec = request.env['sale.order'].search(domain)

这篇关于如何在Odoo 12中的JSON控制器中获取特定记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆