Search code examples
pythonxmlodooodoo-14

How can I display only records that contain a specific value in Odoo one2many view?


I've been having issues with displaying only the records in a relational model that have a field equal to a value that I need to filter them by. Very difficult to word, so I'll paste some code.

Here are the models in question:

    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')

Now, I'm very new to Odoo and databases in general, so the issue could lie in my implementation of the many2one and one2many fields. Here are my views:

    <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>

The function of all of this is to create a module that allows my end-user to split products into a few smaller products. To achieve this aim, this section of the module deals with defining the rules of which products can split into which sources, and how many of each product the sources can be split into (ratio). The user defines a product as having this feature disabled, it being a (repack_)source of a split or being a (repack_)product of a split. The intended behaviour of this one2many field is to display all of the products that can be the product of a split, irrespective of source, to tie a list of records that each source can be split into to that source. This means filtering out any products with repack_status = "repack_disabled" or "repack_source". The difficulty comes in implementing this seemingly-simple behaviour. I have tried: Domains on the one2many/many2one fields, independently and in combination. Domains in the fields in the view, whether at the one2many field or inside the product_id field. I've tried with the context of repack_status. Computing the one2many or the many2one causes them to become readonly, which is obviously not useful, I can't even check if it's filtering correctly.

The attempt I've attached filters correctly, but upon saving the product I get an errror:

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

I've scoured through so many solutions on here and Odoo forums. My code has become somewhat untidy due to all of the different features I've tried. I'm still at a loss as to how this behaviour can be implemented. Any help would be greatly appreciated.

Many thanks in advance


Solution

  • 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
    

    reason for above error is the wrong relation assignment, as you are using one2many field in _inherit = 'product.template' but passing the product.product in

    product_id = fields.Many2one('product.product','Product')
    

    try it by using : product_id = fields.Many2one('product.template','Product')

    also add new field for storing different product in table

    product_temp_id = fields.Many2one('product.template','Product')
    

    and use the above field in view i.e:

    <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>