Skip to Content
Menu
This question has been flagged
12 Replies
15220 Views

Hello,

In a module that I've created, when I click on a menuitem (Weight/number ratio), I execute a python function that returns a treeview. I want to be able to edit a record directly in this treeview.

Python function :

@api.multi

@api.model

def chargement_ratio(self):

        productRatio = self.env['miadi.poidsconditionnement']

        conditionnements = self._cr.execute("SELECT id FROM miadi_packaging")

        query_cond = self.env.cr.dictfetchall()

        produits = self._cr.execute("SELECT id FROM product_product")

        query_prod = self.env.cr.dictfetchall()

        for conditionnements in query_cond:

                nom_conditionnement = conditionnements['id']

                for produits in query_prod:

                        product = produits['id']

                        if_exist = self._cr.execute("SELECT * FROM miadi_poidsConditionnement WHERE conditionnement_id=%s AND produit_id=%s", (nom_conditionnement, product))

                        exist = self.env.cr.dictfetchall()

                        if not exist:

                                Ratio = productRatio.create({'conditionnement_id': nom_conditionnement, 'produit_id': product})

        return {'name': _('Weight/number Ratio'), 'view_type': 'tree', 'view_mode': 'tree', 'view_id': self.env.ref('miadi_packaging.miadi_poidsConditionnement_tree').id, 'res_model': 'miadi.poidsconditionnement', 'type': 'ir.actions.act_window', 'target': 'current',}


XML code :

<record id="miadi_poidsConditionnement_tree" model="ir.ui.view">

        <field name="name">miadi.poidsconditionnement.tree</field>

        <field name="model">miadi.poidsconditionnement</field>

        <field name="arch" type="xml">

                <tree string="Weight/number ratio" editable="bottom">

                        <field name="produit_id" ref="product.product.attribute_value_ids" />

                        <field name="conditionnement_id" ref="miadi.packaging.conditionnement_id" />

                        <field name="nb_articles" />

                        <field name="poids" />

                </tree>

        </field>

</record>

<record model="ir.actions.act_window" id="action_miadi_poidsConditionnement">

        <field name="name">Weight/number ratio</field>

        <field name="type">ir.actions.act_window</field>

        <field name="res_model">miadi.poidsconditionnement</field>

        <field name="view_type">form</field>

        <field name="view_mode">tree,form</field>

        <field name="view_id" ref="miadi_poidsConditionnement_tree"/>

        <field name="help" type="html">

                <p class="oe_view_nocontent_create">Click here to create a weight/number ratio.</p>

        </field>

</record>

<record id="action_chargement_ratio" model="ir.actions.server">

        <field name="name">Chargement Ratio</field>

        <field name="type">ir.actions.server</field>

        <field name="model_id" ref="model_miadi_poidsconditionnement"/>

        <field name="code">action = env['miadi.poidsconditionnement'].chargement_ratio()</field>

</record>

<menuitem name="Weight/number ratio" id="menu_miadi_poidsConditionnement" parent="menu_miadi_packaging" sequence="15" action="action_chargement_ratio" />


Pictures for help :

The treeview returned by the function : https://www.hostingpics.net/viewer.php?id=651454Capturetreeview3.png
What I want to be able to do : https://www.hostingpics.net/viewer.php?id=575162Capturesaisie.png


PS : I'm on Odoo 10


Thanks for answer

Avatar
Discard
Best Answer

Dear Miadi,

Try to change view_type: form  instead of tree

return {'name': _('Weight/number Ratio'), 'view_type': 'form', 'view_mode': 'tree', 'view_id': self.env.ref('miadi_packaging.miadi_poidsConditionnement_tree').id, 'res_model': 'miadi.poidsconditionnement', 'type': 'ir.actions.act_window', 'target': 'current',}




I hope I helped you...
Avatar
Discard
Author

Thank you very much it works !

Best Answer

Hi Miadi,

You can make a tree view editable by adding "editable="top" " in the tree view.

I will attach a sample code where you can edit the tree view.

<tree string="Student" editable="top">

Thank You


<record id="view_admission_student_tree" model="ir.ui.view">
<field name="name">admission.student.tree</field>
<field name="model">course.admission</field>
<field name="priority" eval="8" />
<field name="arch" type="xml">
<tree string="Student" editable="top">
<field name="name" />
<field name="middle_name" />
<field name="last_name" />
</tree>
</field>
</record>
Avatar
Discard
Author

Hi Niyas,

If you look my XML code, you can see I've put : <tree editable="bottom"> but it doesn't work.

First, if you look my pictures (on links), you can see I've not a normal treeview, I can't create or select records

Hi miadi,

You are getting such a tree view because you are returning a tree view from the python as view mode, it would be better if you can add a form view and return view mode as the form view. Then you will get the normal tree view

Author

Hi Niyas, can you give an example please ?

Thanks

Hi, i will add the code in the python, you also have to add a from view in the xml.

list_view_id = imd.xmlid_to_res_id('account.invoice_tree')

form_view_id = imd.xmlid_to_res_id('account.invoice_form')

result = {

'name': action.name,

'help': action.help,

'type': action.type,

'views': [[list_view_id, 'tree'], [form_view_id, 'form'], [False, 'graph'], [False, 'kanban'], [False, 'calendar'], [False, 'pivot']],

'target': action.target,

'context': action.context,

'res_model': action.res_model,

}

Author

and if I already a form view in my xml ?

Here :

<record id="miadi_poidsConditionnement_form" model="ir.ui.view">

<field name="name">miadi.poidsconditionnement.form</field>

<field name="model">miadi.poidsconditionnement</field>

<field name="arch" type="xml">

<form string="Weight/number ratio">

<group col="4">

<field name="produit_id" />

<field name="conditionnement_id" />

<field name="nb_articles" />

<field name="poids" />

</group>

</form>

</field>

</record>

in the python, make view_type as from

Author

Thank you very much it works !

How to make only one field editable in the tree view. And on other field click action open tree view.

Best Answer

how to create the record in tree please as soon as possible i am new 

Avatar
Discard