Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

Define a Product with a Unique Key

Subscribe

Get notified when there's activity on this post

This question has been flagged
purchasewarehousesproduct
3 Replies
12208 Views
Avatar
rahul Hans

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

 

 

 

 

 

0
Avatar
Discard
Avatar
rahul Hans
Author Best Answer

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

1
Avatar
Discard
rahul Hans
Author

Thanks very much for your informative answers

Avatar
Temur
Best Answer

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

 

0
Avatar
Discard
Avatar
Atchuthan - Technical Consultant, Sodexis Inc
Best Answer

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
Avatar
Discard
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Related Posts Replies Views Activity
I want it to bring me only the stores in which this product is available. Not all stores
warehouses product
Avatar
Avatar
1
Jul 25
1187
ODOO COMMUNITY: Multiple Company with same IP address
warehouses product
Avatar
0
Jul 24
1886
Last purchase date Solved
purchase product
Avatar
Avatar
2
Sep 21
4653
Purchase Order and Product name Problem Solved
purchase product
Avatar
Avatar
2
Mar 15
4804
Why does the purchase order allow products where Can be Purchased is false?
purchase v8 product
Avatar
Avatar
1
Nov 24
4770
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now