Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
2 Risposte
14136 Visualizzazioni

I have three table :

- product.

- package.

- product_package.


The product_package table have 2 fields which are related to the product and package as it mentionned bellow :

product_id = fields.Many2one('tjara.product', ondelete='cascade', string="Produit", index=True)
package_id = fields.Many2one('tjara.package', ondelete='cascade', string="Emballage", index=True)
_sql_constraints = [
('ligne_unique', 'unique (product_id, package_id)', '%(package_id)d Package is already exists for this product...!')
]


I added the _sql_contraints to avoid the duplicate fields. Because it can be only an unique relation between the product and a package.

And this is what I have in the product model :

product_package_ids = fields.One2many('tjara.product_package', 'product_id', string='Package')

And this is how to add a package from the product view :

                                <page string="Package">
                                    <label for="product_package_ids" string="Emballage"/>
                                    <field name="product_package_ids">
                                        <tree>
                                            <field name="package_id"/>
                                            <field name="price"/>
                                        </tree>
                                        <form>
                                             <field name="package_id"/>
                                            <field name="price"/>
                                        </form>
                                    </field>
                                </page>


This works fine for me. There is only one thing I want to enhance. When the user select an existing package for the product there is no error triggered. The error popped up only when you click "save the product".

I want either to display only the unexisting packages in the select field package_id. Or to pop up an error when the user try to add an existing package for the product.

 

Avatar
Abbandona
Risposta migliore

You can't achieve it by mere xml domain for 'package_id' or 'product_package_ids' since (1) in the latter there is no reference to a product. (2) when you create a product, it doesn't exist yet.

However, you may add the onchange for 'package_id' (in the method of tjara.product_package) to check uniq:


from odoo import exceptions
@api.multi
@api.onchange("package_id")
def onchange_package_id(self):
  for object in self: 
  if object.product_id:
    existing_ids = self.env["tjara.product_package"].search([('product_id','=',object.product_id.id)], limit=1)
    if len(existing_ids) > 0:
      raise exceptions.Warning(_('Package is already exists for this product...!'))
Avatar
Abbandona
Autore

Thanks a lot IT Libertas,

I just replaced the 'object.product_id' by the product's name and I included a search for the package_id also. So I get the following and it's working well :

@api.onchange('package_id')

def onchange_product_package(self):

for object in self:

if object.product_id:

existing_ids = self.env["tjara.product_package"].search([('package_id', '=', object.package_id.id), ('product_id.name', '=', object.product_id.name)], limit=1)

if len(existing_ids) > 0:

raise exceptions.Warning(('Package is already exists for this product...!'))

Risposta migliore

Hi,

SQL constraints are checked  when odoo send your request to database. In your case when you click the save button.

Use on_change to display alert message when the user change some values (more details).

@api.onchange('package_id',' product_id')
def onchange_product_package(self):
     if self.search([('package_id',='',self.package_id.id),('product_id_id',='',self.package_id.id)]:
         return {'warning': {'title': _('Warning'), 'message': _('your message.')}}

PS: Odoo have already product class and package classes. I don't think, it is a good idea to create new classes and not inherit the native ones.

Best regards.

Avatar
Abbandona
Autore

Thanks a lot Souheil :)

Post correlati Risposte Visualizzazioni Attività
1
mar 19
4541
2
mar 18
4859
8
dic 22
5632
0
mag 17
3028
0
apr 20
5023