如何解决如何在 Odoo one2many 视图中仅显示包含特定值的记录?
我在只显示关系模型中的记录时遇到了问题,这些记录的字段等于我需要用来过滤它们的值。很难说,所以我会粘贴一些代码。
以下是相关模型:
class RepackProductInherit(models.Model):
_inherit = 'product.template'
repack_status = fields.Selection([
('repack_disabled','Repack disabled'),('repack_source','Can be repacked'),('repack_product','Product of repacked cheese')],ondelete={'repack_disabled': 'set default','repack_source': 'set default','repack_product': 'set default'},default='repack_disabled',required=True)
repack_lines = fields.One2many('repack.product.lines','product_id',string="What can this be repacked into?")
class RepackProductLines(models.Model):
_name = 'repack.product.lines'
product_id = fields.Many2one('product.product','Product')
ratio = fields.Integer(string='Ratio')
repack_status = fields.Many2one('product.product','Repack status')
现在,我对 odoo 和一般的数据库非常陌生,所以问题可能在于我对 many2one 和 one2many 字段的实现。 以下是我的看法:
<record id="view_inherit_product_template_form_view" model="ir.ui.view">
<field name="name">product.template.common.form.inherit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='inventory']" position="after">
<page string="Repack" name="repack">
<group>
<field name="repack_status" string="Repack Option"/>
</group>
<field name="repack_lines" context="{'repack_status': repack_status}">
<tree editable="bottom">
<field name="repack_status" invisible="1"/>
<field name="product_id" domain="[('repack_status','=','repack_product')]"/>
<field name="ratio"/>
</tree>
<form>
<group>
<field name="repack_status" invisible="0"/>
</group>
<group>
<field name="product_id"/>
</group>
<group>
<field name="ratio"/>
</group>
</form>
</field>
</page>
</xpath>
</field>
</record>
所有这些的功能是创建一个模块,允许我的最终用户将产品拆分为几个较小的产品。为了实现这一目标,模块的这一部分涉及定义哪些产品可以拆分为哪些来源的规则,以及这些来源可以拆分为每种产品的数量(比率)。用户将产品定义为禁用此功能,它是拆分的 (repack_) 源或拆分的 (repack_) 产品。这个 one2many 字段的预期行为是显示可以成为拆分产品的所有产品,而不管来源如何,以将每个来源可以拆分为该来源的记录列表联系起来。这意味着过滤掉任何带有 repack_status = "repack_disabled" 或 "repack_source" 的产品。困难在于实施这种看似简单的行为。 我努力了: one2many/many2one 字段上的域,独立和组合。 视图中字段中的域,无论是在 one2many 字段还是在 product_id 字段内。 我已经尝试过 repack_status 的上下文。 计算 one2many 或 many2one 导致它们变为只读,这显然没有用,我什至无法检查它是否正确过滤。
我已正确附加过滤器的尝试,但在保存产品时出现错误:
The operation cannot be completed: another model requires the record being deleted. If possible,archive it instead.
Model: repack.product.lines (repack.product.lines),Constraint: repack_product_lines_product_id_fkey
我在这里和 odoo 论坛上搜索了很多解决方案。由于我尝试过的所有不同功能,我的代码变得有些凌乱。我仍然不知道如何实现这种行为。任何帮助将不胜感激。
非常感谢
解决方法
The operation cannot be completed: another model requires the record being deleted. If possible,archive it instead.
Model: repack.product.lines (repack.product.lines),Constraint: repack_product_lines_product_id_fkey
上述错误的原因是关系分配错误,因为您在 _inherit = 'product.template'
中使用 one2many 字段,但在
product_id = fields.Many2one('product.product','Product')
尝试使用:product_id = fields.Many2one('product.template','Product')
还添加了用于在表中存储不同产品的新字段
product_temp_id = fields.Many2one('product.template','Product')
并在视图中使用上述字段,即:
<field name="repack_lines" context="{'repack_status': repack_status}">
<tree editable="bottom">
<field name="repack_status" invisible="1"/>
<field name="product_temp_id" domain="[('repack_status','=','repack_product')]"/>
<field name="ratio"/>
</tree>
<form>
<group>
<field name="repack_status" invisible="0"/>
</group>
<group>
<field name="product_temp_id"/>
</group>
<group>
<field name="ratio"/>
</group>
</form>
</field>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。