コンテンツへスキップ
メニュー
この質問にフラグが付けられました
4 返信
4778 ビュー

Hello,

I present my module (with codes) and after I will explain what I want.

miadi_packaging.py

class miadi_packaging(models.Model):
_name = 'miadi.packaging'
_rec_name = 'conditionnement_id'
_order = 'conditionnement_id asc'
conditionnement_id = fields.Char(string='Code', size=4, default='', required=True)
conditionnement_nom = fields.Char(string='Packaging Name', default='', required=True)
nb_articles = fields.Integer(string='Number of products', default=0, required=True)
poids = fields.Float(string='Weight', default=0, required=True)
type = fields.Many2one('miadi.packaging.type', 'Type of Packaging', default='', required=True)
_sql_constraints = [
('uniq_id', 'unique(conditionnement_id)', 'A packaging already exists with this ID. It must be unique !'),
('uniq_name', 'unique(conditionnement_nom)', 'A packaging already exists with this name. It must be unique !'),
]

miadi_poidsConditionnement.py

class miadi_poidsConditionnement(models.Model):
_name = 'miadi.poidsconditionnement'
conditionnement_id = fields.Many2one('miadi.packaging', 'Packaging', default='', required=True)
produit_id = fields.Many2one('product.product', 'Product', default='', required=True, ondelete='cascade')
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
@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 exist:
if_actif = self._cr.execute("SELECT active FROM product_product WHERE id=%s", (product,))
state_actif = self.env.cr.dictfetchone()
actif = state_actif['active']
if actif == False:
productRatio.search([('produit_id', '=', product), ('conditionnement_id', '=', nom_conditionnement)]).unlink()
if not exist:
if_actif = self._cr.execute("SELECT active FROM product_product WHERE id=%s", (product,))
state_actif = self.env.cr.dictfetchone()
actif = state_actif['active']
if actif == True:
Ratio = productRatio.create({'conditionnement_id': nom_conditionnement, 'produit_id': product})
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',}

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" editable="top">
<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.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_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" />

 

</data>
</openerp>

They are main files.


Now, what I want exactly is to create a python function (I already know how to create a function) but I want, in this function, return a treeview (like in the miadi_poidsConditionnement.py) and display it in a created page in a product.

It is possible ? If yes, how to do this ?

Thanks for answers

PS : I'm on Odoo 10


PS 2: Someone gives me this but I don't really understand how to do this for my case.

Hi Miadi,

To add a field to the product.product model first you have to inherit that model and has to define that model, like this

class ProductProduct(models.Model):
_inherit = 'product.product'

test_field = fields.One2many('test.model', 'connect_test_model', string='Packaging')


class TestModel(models.Model):
_name = 'test.template'

connect_test_model = fields.Many2one('product.template', string="Connect")
customer_id = fields.Char(string="Passenger")
age = fields.Integer(string="Age")

Then in the XML,

<record model="ir.ui.view" id="view_variantes_conditionnement_form">
<field name="model">product.template</field>
<field name="name">view.inherit.variantes.conditionnement.form</field>
<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='notes']" position="after">
<page name="miadi_variantes_conditionnement" string="Packaging">
<field name="test_field" >
<tree editable="bottom">
<field name="customer_id"/>
<field name="age"/>
</tree>
</field>
</page>
</xpath>
</field>
</record>
アバター
破棄

Hi Miadi,

Can you tell me what exactly you want in the newly created page in the product form ?

著作者

Hi Niyas, I want to take records from miadi.poidsconditionnement table for the product where I am. As you can see in the chargement_ratio() function, when I click on the menuitem of the miadi.poidsconditionnement table, it takes all products (from product.product) and all packagings (from miadi.packaging), and if there are not records for the product and the packaging, it creates it.

When I go on the newly created page of the product, I want to takes records for this product from the miadi.poidsconditionnement table and, if in the meantime, some packagings were created, I want to create the records (as I do in the chargement_ratio() function) and display them in the newly create page.

If you don't undertand good, check this :

in miadi.poidsconditionnement table :

for each products and each packagings (with SQL queries) :

if the record doesn't exist:

create it

return the treeview with all records

What I want in the newly created page is :

for the product where I am :

select all records in the miadi.poidsconditionnement table (for this product)

select each packaging (from miadi.packaging)

if the record for this product and a packaging doesn't exist :

create it (in the miadi.poidsconditionnement table)

return the treeview of all records for this product in the newly created page

I hope you will understand what I want and you will be able to help me

Thanks

著作者

I will put this comment as an answer to have the indentation

著作者 最善の回答

Hi Niyas, I want to take records from miadi.poidsconditionnement table for the product where I am. As you can see in the chargement_ratio() function, when I click on the menuitem of the miadi.poidsconditionnement table, it takes all products (from product.product) and all packagings (from miadi.packaging), and if there are not records for the product and the packaging, it creates it.

When I go on the newly created page of the product, I want to takes records for this product from the miadi.poidsconditionnement table and, if in the meantime, some packagings were created, I want to create the records (as I do in the chargement_ratio() function) and display them in the newly create page.

If you don't undertand good, check this :

in miadi.poidsconditionnement table :

for each products and each packagings (with SQL queries) :
    if the record doesn't exist:
        create it
return the treeview with all records

What I want in the newly created page is :

for the product where I am :
    select all records in the miadi.poidsconditionnement table (for this product)
    select each packaging (from miadi.packaging)
    if the record for this product and a packaging doesn't exist :
        create it (in the miadi.poidsconditionnement table)
return the treeview of all records for this product in the newly created page

I hope you will understand what I want and you will be able to help me

Thanks

アバター
破棄