Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
3 Odpovědi
11437 Zobrazení

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

 

 

 

 

 

Avatar
Zrušit
Autor Nejlepší odpověď

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

Avatar
Zrušit
Autor

Thanks very much for your informative answers

Nejlepší odpověď

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

 

Avatar
Zrušit
Nejlepší odpověď

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.

 

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
1
čvc 25
713
0
čvc 24
1164
2
zář 21
3973
2
bře 15
4123
1
lis 24
4202