Skip to Content
Menu
This question has been flagged
7 Replies
7589 Views

Hello everybody,

In a module that I've created (module miadi_packaging) with two classes in the python file (miadi_packaging and miadi_poidsConditionnement) (miadi_packaging.py), I want to execute python code when I access to the "miadi_poidsConditionnement" page in Odoo.

More explanations :

In Odoo, in the module Sales, I've two menus that i've created : Sales packaging and Weight/Number Ratio.

In Sales Packaging, I can create special packaging and in Weight/Number Ratio I can create ratios for a product and a packaging and define the weight and the number of products.

I want to execute code when I click on Weight/Number Ratio.

How can I make it ?

I don't know if I'm really understandable but I hope somebody wiil can help me.

Thanks for answers

Avatar
Discard
Best Answer

Hi Miadi,

You want to call the python function when clicking the menu or when opening the form ?

If you want to call python function on clicking the menu, You can check this link, here it is a server action from the menu item.

If you want to call python function on loading a form view, you can give compute function to any of the field in the view. Compute function will get called once the view is called.

class RespartnerExtend(models.Model):
_inherit = 'res.partner'

sample_field = fields.Char(string="User", compute='call_function')

@api.depends('sample_field')
def call_function(self):
#write the function body here


Avatar
Discard
Author

Hi, Niyas,

I whant to call python function on cliking the menu (at the left of the Odoo interface).

Can you explain me how to do it please ? Because on the link you gave, there is for old version but I'm on the Odoo 10...

Regards,

MIADI

Author

Python Code (tell me if it's good please) :

class miadi_poidsConditionnement(models.Model):

_name = 'miadi.poidsconditionnement'

conditionnement_id = fields.Char('Packaging', size=4, default='', required=True)

produit_id = fields.Char('Product', size=256, default='')

nb_articles = fields.Integer(string='Number of products', default=0)

poids = fields.Float(string='Packaging Weight', default=0)

_sql_constraints = [

('uniq_id', 'unique(produit_id, conditionnement_id)', 'A product already exists with this packaging !'),

]

@api.model

@api.multi

def chargement_ratio():

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

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

products = self.env['product.product'].search([])

for conditionnements in self.env.cr.dictfetchall():

nom_conditionnement = conditionnements['name']

for rec in products:

product = rec.display_name

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

I want to execute the function when I click on the menuitem "Weight/Number Ratio" : chargement_ratio()

Author

The identation is good in my code, just here it's a copy/paste so it donn't take the indentation

identation wont work out while copy paste in comment, but works will in answer post.let me check with my works, i will provide you sample in new api

Author

Hi Niyas, have you found that you want to show me ?

Hi Miadi, actually you can refer the link right ? just you have to change the function to new api,

for that just add @api.multi before the function

Author

Hi Niyas, I refer to the link but in "<field name="model_id" ref="miadi_poidsConditionnement" />" I an issue that tell me "External ID not found in the system: miadi_packaging.miadi_poidsConditionnement"

My module is like this :

name of module : miadi_packaging

in the folder "miadi_packaging" I've : miadi_packaging.py and miadi_poidsConditionnement.py and the function I want to execute is in the class miadi_poidsConditionnement (in the miadi_poidsConditionnement.py).

I don't know why I have this issue

Author

PS : look the answer that I give below these comments

Author Best Answer

Hi Niyas,

I tried that I've found in your link. I've made this but it's gives me this error : ParseError:
"External ID not found in the system: miadi_packaging.miadi_poidsConditionnement" while parsing file:///C:/Program Files (x86)/Odoo 10.0/server/odoo/addons/miadi_packaging/views/miadi_poidsConditionnement_view.xml:62, near

<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_PC"/>
                        <field name="code">action = self.chargement_ratio()</field> </record>


There is my miadi_poidsConditionnement.py :

import odoo from odoo import models, fields, tools, api

class miadi_poidsConditionnement(models.Model):

    _name = 'miadi.poidsconditionnement' 

    conditionnement_id = fields.Char('Packaging', size=4, default='', required=True) 

    produit_id = fields.Char('Product', size=256, default='')

    nb_articles = fields.Integer(string='Number of products', default=0)

    poids = fields.Float(string='Packaging Weight', default=0)

    _sql_constraints = [

        ('uniq_id', 'unique(produit_id, conditionnement_id)', 'A product already exists with this packaging !'),


     @api.multi

    def chargement_ratio(self):

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

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

            products = self.env['product.product'].search([])

            for conditionnements in self.env.cr.dictfetchall():

                    nom_conditionnement = conditionnements['name']

                    for rec in products:

                            product = rec.display_name

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


And my XML file where are the views of the menuitem on which I want to do the action (miadi_poidsConditionnement_view.xml) :

<?xml version="1.0" encoding="utf-8"?>

<openerp>

 <data>

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

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

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

     <field name="produit_id" />

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

     <field name="conditionnement_id" />

     <field name="nb_articles" />

     <field name="poids" />

    </tree>

   </field>

  </record>

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

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

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

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

    <search string="Weight/number ratio">

     <field name="produit_id" />

     <group expand="0" string="Group by">

      <filter string="Product" domain="[]" context="{'group_by':'produit_id'}"/>

      <filter string="Packaging" domain="[]" context="{'group_by':'conditionnement_id'}"/>

     </group>

    </search>

   </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_PC" /> 

   <field name="code">action = self.chargement_ratio()</field>

  </record>

  <menuitem name="Weight/number ratio" id="menu_miadi_poidsConditionnement" parent="menu_miadi_packaging" sequence="15" groups="sales_team.group_sale_manager" />

  <menuitem id="menu_action_miadi_poidsConditionnement" parent="menu_miadi_packaging" action="action_miadi_poidsConditionnement" />

<menuitem id="menu_action_chargement_ratio" parent="menu_miadi_poidsConditionnement" action="action_chargement_ratio" />

 </data>

</openerp>


Thanks if you found the solution.

Regards,

MIADI

Avatar
Discard