跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 开发与实施交流
  4. Odoo10 怎么在onchange里面为one2many赋值?

Odoo10 怎么在onchange里面为one2many赋值?

已定时 已固定 已锁定 已移动 Odoo 开发与实施交流
8 帖子 2 发布者 2.6k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • J 离线
    J 离线
    jalena
    写于 最后由 编辑
    #1

    我现在有这样的一个模型

    Class A:
    fields.one2many('a')

    Class B:
    fields.one2many('c')

    Class 😄
    fields.many2one('product.product')

    现在的需求是,在 Class A里面有一个onchange方法,他是根据客户的改变,带出他的one2many(这里的one2many数据,包含下级的,都是从其他地方取过来赋值的),,请问这样的数据,怎么操作呢。。

    1 条回复 最后回复
    0
    • 萧 离线
      萧 离线
      萧云飞
      写于 最后由 编辑
      #2

      one2many的赋值规则是[(0,0,{}),...],比如你的one2many字段为field_c,那么你的onchange方法中赋值方式为:
      field_c = [(0, 0, {'name': 'Test', ...})]

      J 2 条回复 最后回复
      0
      • J 离线
        J 离线
        jalena
        在 回复了 萧云飞 最后由 编辑
        #3

        @萧云飞 这个我现在知道,我现在的model其实是one2many里面还有一个one2many。。。第一个one2many赋值没有问题,但第二个one2many就有问题了

        1 条回复 最后回复
        0
        • J 离线
          J 离线
          jalena
          在 回复了 萧云飞 最后由 编辑
          #4

          @萧云飞 我现在的代码是这样的。

          	def _materialValue(self):
          		page = []
          
          		for item in self.suite_series.main_material_ids:
          			product_ids = []
          			data = {
          				'location': item.area_id.id,  # 区域位置
          				'name': item.project_record_id.name,  # 项目名称
          				'model': item.model_type,  # 型号
          				'brand': item.product_brand_id.id,  # 品牌
          				'specifications': item.specifications,  # 规格
          				'price': item.price,  # 单价
          				'number': item.number,  # 数量
          				'unit': item.unit.id,  # 单位
          				# 'product_ids': False,
          				'total_price': item.total_price,  # 合价
          				'remarks': item.general_description,  # 备注
          				'attribute': item.attribute_value_ids  # 属性
          			}
          
          			# 遍历材料  0,false,[object Object],0,false,[object Object]
          			for val in item.product_id:
          				line_item = {
          					'product_id': val.product_id.id,  # 材料
          					'classify': val.pro_type  # 分类
          				}
          				product_ids.append((0, 0, line_item))
          			data.update({'product_ids': product_ids})
          			page.append((0, False, data))
          		return page
          

          返回的数据是:

          < type 'list' >: [(0, 0, {
          	'specifications': u '\u89c4\u683c',
          	'attribute': product.attribute.value(),
          	'price': 100.0,
          	'number': 1,
          	'remarks': False,
          	'unit': 8,
          	'total_price': 10000.0,
          	'name': u '\u6728\u5730\u677f',
          	'location': 12,
          	'model': u '\u578b\u53f7',
          	'brand': 1,
          	'product_ids': [(0, 0, {
          		'product_id': 25,
          		'classify': False
          	}), (0, 0, {
          		'product_id': 33,
          		'classify': False
          	})]
          }), (0, 0, {
          	'specifications': u '12',
          	'attribute': product.attribute.value(),
          	'price': 237500.0,
          	'number': 112,
          	'remarks': False,
          	'unit': 8,
          	'total_price': 0.0,
          	'name': u '\u74f7\u7816',
          	'location': 12,
          	'model': u '12',
          	'brand': 1,
          	'product_ids': [(0, 0, {
          		'product_id': 32,
          		'classify': False
          	}), (0, 0, {
          		'product_id': 33,
          		'classify': False
          	})]
          })]
          

          onchange方法

          	@api.onchange('suite_series')
          	def onchange_suite_series(self):
          		"""套装改变"""
          		if self.main_materials_list or self.basic_materials_list or self.basic_incidentals:
          			return {'warning': {
          				'title': '警告',
          				'message': '材料已经存在'
          			}}
          
          		self.main_materials_list = self._materialValue()
          
          萧 1 条回复 最后回复
          0
          • 萧 离线
            萧 离线
            萧云飞
            在 回复了 jalena 最后由 编辑
            #5

            @jalena 这个规则是针对一个对象one2many和many2many赋值的,所以你第一层这样赋值没有问题,但是第二层,你的对象是谁,这个数据包,它自己区分么?我想这是一个问题,我没有这样赋值过,你可以在下层对象再写一个onchange,然后在这样赋值,我想就解决了

            J 2 条回复 最后回复
            0
            • J 离线
              J 离线
              jalena
              在 回复了 萧云飞 最后由 编辑
              #6

              @萧云飞 那我能不能直接new一个对象进去呢

              1 条回复 最后回复
              0
              • J 离线
                J 离线
                jalena
                在 回复了 萧云飞 最后由 编辑
                #7

                @萧云飞 感谢您哈。做出来了,第一层赋值确实如你所说,第二次我就只能重写create方法来写入数据了!

                	@api.onchange('suite_series')
                	def onchange_suite_series(self):
                		"""套装改变"""
                		if self.main_materials_list or self.basic_materials_list or self.basic_incidentals:
                			return {'warning': {
                				'title': '警告',
                				'message': '材料已经存在'
                			}}
                		self.basic_incidentals = self._incidentals()
                		self.main_materials_list = self._materialValue()
                		self.basic_materials_list = self._basicMaterial()
                
                	@api.multi
                	def _materialValue(self):
                		"""返回主材数据的model"""
                		page = list()
                		for item in self.suite_series.main_material_ids:
                			# 主材项目数据
                			data = {
                				'location': item.area_id.id,  # 区域位置
                				'name': item.project_record_id,  # 项目名称
                				'model': item.model_type,  # 型号
                				'brand': item.product_brand_id.id,  # 品牌
                				'specifications': item.specifications,  # 规格
                				'price': item.price,  # 单价
                				'number': item.number,  # 数量
                				'unit': item.unit.id,  # 单位
                				'total_price': item.total_price,  # 合价
                				'remarks': item.general_description,  # 备注
                				'attribute': item.attribute_value_ids  # 属性
                			}
                			page.append((0, 0, data))
                		return page
                
                	@api.model
                	def create(self, values):
                		"""重写保存方法"""
                		result = super(BudgetMainMaterial, self).create(values)
                		old_material = self.env['material'].search([('main_material_id', '=', values[u'name'])])
                		for material in old_material:
                			self.env['budget.material'].create({'product_id': material.product_id.id, 'main_id': result.id})
                
                萧 1 条回复 最后回复
                0
                • 萧 离线
                  萧 离线
                  萧云飞
                  在 回复了 jalena 最后由 编辑
                  #8

                  @jalena 在 Odoo10 怎么在onchange里面为one2many赋值? 中说:

                  name

                  不客气,您实现的思路也很好

                  1 条回复 最后回复
                  0

                  • 登录

                  • 没有帐号? 注册

                  • 登录或注册以进行搜索。
                  • 第一个帖子
                    最后一个帖子
                  0
                  • 版块
                  • 标签
                  • 热门
                  • 用户
                  • 群组