This question has been flagged

Hello!

I have a form view with two buttons. One button – Create (type=object) – should add a record to DB. Second one – View (type=action) – should open tree view with all created records.

The problem is that Create button adds two similar records to DB and View button opens tree view and also adds one record to DB. View button doesn’t even use method from my model.

What can be a problem with duplication of records?

Odoo v14

Here is the code

Views:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
    
    <!-- ADD BUTTON TO SETTINGS -->

    <record id="res_config_settings_layouts_view_form" model="ir.ui.view">
            <field name="name">res.config.settings.layouts.view.form.inherit.base.setup</field>
            <field name="model">res.config.settings</field>
            <field name="inherit_id" ref="base_setup.res_config_settings_view_form"/>
            <field name="arch" type="xml">
                <xpath expr="//div[@id='business_documents']" position="inside">    
                    
                    <div class="row mt16 o_settings_container" name="business_documents_setting_container">
                        <div class="col-12 col-lg-6 o_setting_box" id="paper_format_setting">
                            <div class="o_setting_right_pane">
                                <span class="o_form_label">Configurable Layouts</span>
                                <div class="text-muted">
                                    Set layouts for printed documents
                                </div>
                                <div class="content-group">
                                    <div class="mt8">
                                        <button name="%(custom_layouts.action_custom_layout_configurator)d" string="Layouts Settings" type="action" class="oe_link" icon="fa-arrow-right"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </xpath>
            </field>
        </record>

    <!-- FORM VIEW -->

    <record id="view_custom_layouts_form" model="ir.ui.view">
            <field name="name">document.custom.layouts</field>
            <field name="model">custom.layouts</field>
            <field name="arch" type="xml">
                <form string="Select something to som..." create="false" edit="false">
                    <group>
                        <field name="report_layout_id"/>
                        <field name="paperformat_id"/>
                    </group>
                    <div>
                        <button string="Add this" name="add_linked_docs" type="object" class="btn btn-primary"/>
                        <button string="View" name="%(custom_layouts.action_custom_layout_list)d" type="action" class="btn btn-seconadry ml-2"/>
                    </div>
                </form>
            </field>
        </record>

        <record id="action_custom_layout_configurator" model="ir.actions.act_window">
            <field name="name">Configure your document layout</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">custom.layouts</field>
            <field name="view_mode">form</field>
            <field name="target">new</field>
            <field name="view_id" ref="custom_layouts.view_custom_layouts_form"/>
        </record>
        
    <!-- TREE VIEW -->

        <record id="view_custom_layouts_tree" model="ir.ui.view">
            <field name="name">document.custom.layouts.tree</field>
            <field name="model">custom.layouts</field>
            <field name="arch" type="xml">
                <tree string="Tree" create="false" edit="false">
                    <field name="id"/>
                    <field name="report_layout_id"/>
                    <field name="paperformat_id"/>
                </tree>
            </field>
        </record>

        <record id="action_custom_layout_list" model="ir.actions.act_window">
            <field name="name">Review the list</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">custom.layouts</field>
            <field name="target">new</field>
            <field name="view_mode">tree</field>
            <field name="context">{'group_by': ['report_layout_id']}</field>
            <field name="view_id" ref="custom_layouts.view_custom_layouts_tree"/>
        </record>

    </data>
</odoo>

Model


from odoo import api, fields, models, _

class SetCustomLayouts(models.Model):
    _name = 'custom.layouts'
    _description = 'Document Layout settings'
    # _order = 'report_layout_id'

    report_layout_id = fields.Many2one('report.layout')
    paperformat_id = fields.Many2one('ir.actions.report'string="Report")

    def add_linked_docs(self):
        vals = {
            'report_layout_id' : self.report_layout_id.id,
            'paperformat_id' : self.paperformat_id.id
        }
        self.create(vals)

Form view


result


Avatar
Discard
Author Best Answer

Fixed.

There should be two separate models - one Transient for creation, and one Model to store data.

Avatar
Discard