Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Define a Product with a Unique Key

By
rahul Hans
on 2/9/15, 9:41 AM 2,358 views

Hi, 

I am new to OpenErp. But I am conversant with ERP and with the functional areas of a business. I am working with a retail company that sells accessories and items of clothing amongst other products.

We need to create a product with a unique key. On my attempt to create a product I found I was able to create more than one salable product with 

1. Same Name

2. Same EAN13 barcode

My questions are very basic and is related to products. More specifically to definition of products. 

I need to create a product with a unique bar code. In other words, once defined with a 13 digit bar code, it should no tbe possible to create another product with the same bar code. Ideally this should also happen in case of product name.

1. Is there any way to achieve this in OpenERP?

2. What is the approach that OpenErp uses in defining unique products?

3. Is there any way to define a field in OpenErp that can be unique. We should be able to use the field to search a product and the field should also be usable by a bar code scanner?

I hope my questions are clear. In case of any doubts, please do let me know.

An early response would be appreciated

 

 

 

 

 

1
rahul Hans
On 2/9/15, 11:26 AM

Okay. First of all can thanks for your answer. 

I understand Sql constraints.

What are the exact differences between _constraints & @api_constraints? I can speculate on the differences 

A brief but reply would be most helpful. 

Also I tried searching for api constraints. Could not find anything useful. Can you point me in the right direction?

Thanks once again

Thanks very much for your informative answers

rahul Hans
on 2/10/15, 9:45 AM
0

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3918
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 2/10/15, 7:01 AM

Constraints are applied only using ORM methods i.e. only in code and it does not affect the table whereas _sql_constraints set the rule at table level

In Odoo 7:
_sql_constraints and _constraints arguments were used

In Odoo 8:
_sql_constraints and @api.constraints arguments are used in v8.

 

0

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 2/9/15, 10:25 AM

There is several ways to achieve that, please check v8.0 documentation or v7.0 documentation selon your needs. possible options are "_constraints", "_sql_constraints" and "@api.constrains" (the last option is v8 speciphic and it replaces v7 _constraints, which is deprecated in v8 ). 

 

EXAMPLES:

  • _sql_constraints example of usage in "stock.py":

    _sql_constraints = [
            ('warehouse_name_uniq', 'unique(name, company_id)', 'The name of the warehouse must be unique per company!'),
            ('warehouse_code_uniq', 'unique(code, company_id)', 'The code of the warehouse must be unique per company!'),
        ]
  • @api.constrain example of usage in "event.py":

        @api.one
        @api.constrains('event_id', 'state', 'nb_register')
        def _check_seats_limit(self):
            if self.event_id.seats_max and \
                self.event_id.seats_available < (self.nb_register if self.state == 'draft' else 0):
                    raise Warning(_('No more available seats.'))
     
  • _constraints example of usage in "stock.py":

        def _check_uom(self, cr, uid, ids, context=None):
            for move in self.browse(cr, uid, ids, context=context):
                if move.product_id.uom_id.category_id.id != move.product_uom.category_id.id:
                    return False
            return True

        _constraints = [
            (_check_uom,
                'You try to move a product using a UoM that is not compatible with the UoM of the product moved. Please use an UoM in the same UoM category.',
                ['product_uom']),
        ]

 

The code in "_constraints" example above may be rewritten with "@api.constrains" as follows:

    @api.one
    @api.constrains('product_uom')
    def _check_uom(self):
        if self.product_id.uom_id.category_id.id != self.product_uom.category_id.id:
            raise Warning(_('You try to move a product using a UoM that is not compatible with the UoM of the product moved. Please use an UoM in the same UoM category.'))

 

_constraints and @api.constrains are the same concept and they allow to validate field in any sense (not only for uniqueness) using python function (as _check_uom - in examples above). _constraints is still supported but deprecated in v8, whereas @api.constrains makes part of new v8 api as its replacement

 

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/9/15, 9:41 AM
Seen: 2358 times
Last updated: 3/16/15, 8:10 AM