有这么些个数据模型结构,产品product.template模型下,有One2many类型字段attribute_line_ids指向product.attribute.line数据模型,在product.attribute.line数据模型中,有Many2one字段 product_id指回 product.template模型,还有Many2one字段attribute_id指向product.attribute数据模型,还有Many2many类型字段attribute_value_ids指向product.attribute.value模型。如下:

class ProductTemplate(models.Model):_name = "product.template"name = fields.Char(string="Name")attribute_line_ids = fields.One2many("product.attribute.line",string="Attributes")class ProductAttributeLine(models.Model):_name = "product.attribute.line"product_id = fields.Many2one('product.template', string='Product ID')attribute_id = fields.Many2one('product.attribute', string="Attr Name")attribute_value_ids = fields.Many2many('product.attribute.value', string="Attr Values")class ProductAttribute(models.Model):_name = "product.attribute"name = fields.Char('Name', required=True, translate=True)value_ids = fields.One2many('product.attribute.value', 'attribute_id', 'Values', copy=True)attribute_line_ids = fields.One2many('product.attribute.line', 'attribute_id', 'Lines')class ProductAttributeValue(models.Model):_name = "product.attribute.value"name = fields.Char('Value', required=True, translate=True)attribute_id = fields.Many2one('product.attribute', 'Attribute', ondelete='cascade', required=True)

创建产品及增加属性,得到3个产品

产品名(name) 测试1  
属性明细(attribute_line_ids ) 属性名(attribute_id.name ) 属性值(attribute_line_ids.name)
  外壳颜色 红色/白色(两个值)
  底壳颜色 白色
  尺寸 13寸
     
产品名(name) 测试2  
属性明细(attribute_line_ids ) 属性名(attribute_id.name ) 属性值(attribute_line_ids.name)
  上壳颜色 红色
  底壳颜色 白色
  尺寸 15寸
     
产品名(name) 测试3  
属性明细(attribute_line_ids ) 属性名(attribute_id.name ) 属性值(attribute_line_ids.name)
  上壳颜色 白色
  底壳颜色 红色/白色(两个值)
  尺寸 15寸

1 假如只知道要查询的产品有一个属性值是白色(不知道属性名是什么,只知道属性值是白色)

    obj = self.env["product.template"]domain = [("attribute_line_ids.attribute_value_ids.name","=","白色")]res = obj.sudo().search(domain)

这样,会查询出3条记录。

2 假如知道要查询的产品属性值既有红色,又有白色(还是不知道属性名是什么)

    obj = self.env["product.template"]domain = [("attribute_line_ids.attribute_value_ids.name","=","红色"),             ("attribute_line_ids.attribute_value_ids.name","=","白色")]res = obj.sudo().search(domain)

这样查出来的记录仍会是3条。

3 假如知道要查询的产品属性值既有红色,又有白色(还是不知道属性名是什么),但还知道尺寸是15寸

    obj = self.env["product.template"]domain = [("attribute_line_ids.attribute_value_ids.name","=","红色"),("attribute_line_ids.attribute_value_ids.name","=","白色"),("attribute_line_ids.attribute_value_ids.attribute_id.name","=","尺寸"),("attribute_line_ids.attribute_value_ids.name","=","15寸")]res = obj.sudo().search(domain)

这样就可以拿到 测试1 测试2 两条产品数据。

4 假如知道要查询的产品的底壳颜色只有白色,尺寸是13寸

    obj = self.env["product.template"]domain = [("attribute_line_ids.attribute_id.attribute_value_ids.name","=","底壳颜色"),("attribute_line_ids.attribute_value_ids.name","=","白色"),("attribute_line_ids.attribute_value_ids.attribute_id.name","=","尺寸"),("attribute_line_ids.attribute_value_ids.name","=","15寸")]res = obj.sudo().search(domain)

很遗憾,这样并不能过滤出底壳颜色只有白色的13寸尺的产品。因为再M2M字段中,使用这种方式去过滤,并不严谨。当一条属性记录中的属性名为底壳颜色,但对应的属性值为 红色、白色 两条记录时。使用上面的代码,会将所有包含 底壳颜色=白色 的记录都拿到。只要 底壳颜色=白色 存在就行,其他你有什么我都不管,因为 底壳颜色=红色 和 底壳颜色=白色 是两条单独的数据。

那怎么办?再加一条 ("attribute_line_ids.attribute_line_ids.name","!=","红色") ?不要,这样解决不了上面说的问题,因为 底壳颜色 = 白色 已经满足了 ("attribute_line_ids.attribute_line_ids.name","=","白色") 条件,而且 底壳颜色 = 白色 也满足了 ("attribute_line_ids.attribute_line_ids.name","!=","红色") 的条件。

到底怎么办?如果非要使用这种过滤方式,我个人建议(因为我不知道其他的方式)按上面的代码先缩小搜索范围,然后对得到的结果数据集做一次filtered,对结果数据集进行二次过滤,就像下面这样:

    for item in res:if item.attribute_line_ids.filtered(lambda l: l.attribute_id.name == '底壳颜色' and len(l.attribute_line_ids) == 1 and l.attribute_line_ids.name == '白色'):# do something matchedpasselse:# do something un-matchedpass

如发现问题,欢迎指正

【ODOO】如何使用search查询匹配多个属性值的产品记录相关推荐

  1. 【ES6新特性】一行代码解决:搜索对象数组,匹配具体字段属性值的返回值和索引的问题

    arr.find(v => v.key == "需要搜索的值") //返回搜索匹配字段属性值的对象arr.findIndex(v => v.key == "需 ...

  2. vector怎么按字段查询顺序输出_7大查询匹配类函数,一次给你总结好

    Excel数据处理中,经常用到各种函数,可以说函数是Excel必不可少的一部分,今天向大家介绍数据处理中的七个查询匹配函数. 下面一一介绍各函数的具体用法. vlookup 功能:搜索表区域首列满足条 ...

  3. elasticsearch-es search 查询

    elasticsearch-es search 查询 elasticsearch-es search 搜索 准备 基于url 基于请求主体 query DSL filter过滤器 DSL term精确 ...

  4. Elasticsearch Search API之(Request Body Search 查询主体)

    作者介绍:<RocketMQ技术内幕>作者,中间件兴趣圈微信公众号维护者,文末有对应的二维码,关注后可以与作者更好的互动. 本文有点长,看完可能需要点耐心,本文详细介绍了es三种分页方式. ...

  5. vlookup匹配值不唯一_数据对比、数据查询匹配Vlookup函数3种常见错误及解决方案...

    Excel中的Vlookup函数,在大家日常数据处理计算中应用的机会非常多,因为它可以帮助我们完成数据查询匹配.数据对比.但是这个函数在使用的过程中也经常会遇到查询错误的问题.根据实践经验总结,发现主 ...

  6. mysql函数 用来查询匹配不到的数据_详解MySql基本查询、连接查询、子查询、正则表达查询...

    select * from STUDENT; 2.2.按条件查询 (1) 比较运算符 > , < ,= , != (< >),>= , <= select * fr ...

  7. 文档查询匹配的方法:布尔模型,向量空间模型

    文档查询匹配的方法:布尔模型,向量空间模型 文档表示:词袋方法Bag-of-Words Approach • 在 IR 中表示文档(和查询)的传统方法: 记录出现的单词(术语; 通常,加上每个文档中的 ...

  8. mysql 查询语句属性值_MySQL学习——SQL查询语句(一)

    查询数据是指从数据库中获取所需要的数据,查询数据是数据库操作中最常用也是最重要的操作,用户可以根据对数据的需求,使用不同的查询方式,通过不同的查询方式可以获得不同的数据,在MySQL中使用select ...

  9. 图标选择器_【小技巧】巧用CSS属性值正则匹配选择器

    属性值正则匹配选择器包括下面3种: [attr^="val"] [attr$="val"] [attr*="val"] 这3种属性选择器是字 ...

最新文章

  1. UE把环境变量Path改了
  2. C++的常量、指针、引用
  3. p1209 Barn Repair
  4. 费曼学习法:为何被称为史上最牛的学习法,它的本质究竟是什么?
  5. python线下课程厦门_厦门python课程
  6. html jq 控制显示密码,js、jquery分别实现点击密码输入框密码显示和隐藏
  7. Live Performer for Mac(音频演奏录制软件)v1.0.1
  8. 论如何优雅的处理回文串 - 回文自动机详解.
  9. screenfull.js跨浏览器使用JavaScript Fullscreen API
  10. Citrix 桌面云 XenApp_XenDesktop_7.15 部署系列(六)配置虚拟桌面控制器
  11. 上海工程技术大学c语言商店存货管理系统,商店存货管理系统解决方案.doc
  12. qomo linux最新版本,Qomo Linux下一个版本将推驱动中心
  13. 华为平均每天收入23.5亿元!重磅发布2019年年报!
  14. java der格式_读取DER格式java中的私钥
  15. oracle11g 企业管理器无法登陆
  16. 移动硬盘误删分区,数据完美找回
  17. After Effects CC 2019 Essential Training: The Basics After Effects CC 2019基础教程:基础知识 Lynda课程中文字幕
  18. 解决uniapp微信开发小程序背景本地图片渲染层网络层错误问题
  19. 【C语言程序】输入一个字符串,将其反转并输出
  20. 各种排序的时间复杂度、空间复杂度、稳定性

热门文章

  1. 网易青媒第11期 | 第2节课笔记(内容创作:如何让你的故事直击人心)
  2. 【转载】2020年中国乘用车前视系统TOP10供应商,本土仅经纬恒润入围!
  3. 公共数据运营的框架设计
  4. fpga千兆以太网学习2
  5. 测量正弦电压信号之自制并行比较型ADC(TINA仿真)测量正弦电压之VFC模块(电压转频率,proteus仿真)
  6. (四)Python反爬实战---JS反爬之某网站portal-sign验证参数加密
  7. c# 多张图片合成一张图片
  8. 架构师实战培训(架构设计+数据后台+快速开发+二次开发+发布部署)
  9. 土地资源管理就业怎么这么难_【土地资源管理研究生就业怎么样,有发展么?】-看准网...
  10. 文字一键转语音,配音不用愁,支持几十种男声女声!