콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다

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.

 

아바타
취소
베스트 답변

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...!'))
아바타
취소
작성자

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...!'))

베스트 답변

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.

아바타
취소
작성자

Thanks a lot Souheil :)

관련 게시물 답글 화면 활동
1
3월 19
4542
2
3월 18
4860
8
12월 22
5646
0
5월 17
3028
0
4월 20
5031