如何过滤odoo8中的many2many字段?

时间:2016-05-20 22:12:09

标签: openerp

我有以下型号,并且扩展到product_template

class Version(models.Model):
    _name='product_cars_application.version'

    name = fields.Char()
    model_id = fields.Many2one('product_cars_application.model',string="Model")
    brand_id = fields.Char(related='model_id.brand_id.name',store=True,readonly=1)
    year_id = fields.Char(related='model_id.year_id.name',store=True,readonly=1)

from openerp.osv import osv,fields as Fields

class product_template(osv.osv):
    _name = 'product.template'
    _inherit = _name
    _columns = {
        'versions_ids':Fields.many2many('product_cars_application.version',string='Versions')
    }

以下控制器我需要按version_id过滤产品

@http.route('/pa/get_products/<version_id>', auth='none', type='json',website=True)
    def get_products(self,version_id,**kwargs):
        #TODO APPEND SECURITY
        version_id = int(version_id)
        products = http.request.env['product.template'].sudo().search([(version_id,'in','versions_ids')])

当version_id在versions_ids中时,我得不到任何产品。 有谁知道我做错了什么?

2 个答案:

答案 0 :(得分:1)

我需要将字段的比较值作为列表,也许因为字段versions_ids是一个很多的

我这样解决了:

@http.route('/pa/get_products/<version_id>', auth='none', type='json',website=True)
def get_products(self,version_id,**kwargs):
    #TODO APPEND SECURITY
    products = http.request.env['product.template'].sudo().search([('versions_ids','in',[version_id])])
    list = []
    for p in products:
        list.append([p.id, p.name])
    return {
        'products':list,
    }

答案 1 :(得分:0)

&#34; return products.ids &#34;在@http.route('/pa/get_products/<version_id>', auth='none', type='json',website=True) def get_products(self,version_id,**kwargs): #TODO APPEND SECURITY version_id = int(version_id) products = http.request.env['product.template'].sudo().search([(version_id,'in','versions_ids')]) return products.ids 内遗失,如:

{{1}}