Odoo Help

2

1
6 Answers
6
Avatar

Emipro Technologies Pvt. Ltd. India

--Emipro Technologies Pvt. Ltd.--
7823
| 7 8 10
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Emipro has been providing a wide range of business solutions in the domain of ERP & eCommerce. We take pride in serving 1000s of contented customers across the globe with strong roots in the US, Europe & India. Being a Certified Odoo Gold Partner in India as well as the United States, we bring our rich professional experience and unrivaled technical competence to help our customers obtain efficient and cost-effective business solutions in Odoo. With our team of 50+ Odoo Experts and highly experienced business analysts, we serve our customers which are companies of all sizes, ranging from start-ups to large enterprises. 

Our ERP Services:

  • Business Need Analysis & GAP Analysis

  • ERP Migration

  • Odoo Consultancy

  • Odoo Implementation & Customization

  • Odoo Training & Support

  • Odoo Website & eCommerce Development

  • Odoo Integration (Marketplaces, eCommerce platforms, Shipping Carriers, Payment Gateways etc.)

Our Achievements:


Reach Us:

Website: www.emiprotechnologies.com

Email: info@emiprotechnologies.com

Skype: emiprotech

Emipro Technologies Pvt. Ltd. India
7/13/15, 8:25 AM

Hello,

If you want to restrict user for some operations within the limitation of python program itself then you need to define _constraint attribute in any model. 

For knowledge :- For restriction of data inputs from database side, you need to define _sql_constraint attribute within your model.

Example of _constraint :

That attribute needs 3 arguments :

1. Function ( Which will be called to restrict user for some operations )

2. Warning ( Which will be shown to user when condition will be overruled )

3. List of fields ( If you want to show affected field names to user )

go to project.py of project module. There is following constraint,

    _constraints = [
(_check_dates, 'Error! project start-date must be lower than project end-date.', ['date_start', 'date'])
]

_check_dates is the function, definition of that will be as follows,

    def _check_dates(self, cr, uid, ids, context=None):
for leave in self.read(cr, uid, ids, ['date_start', 'date'], context=context):
if leave['date_start'] and leave['date']:
if leave['date_start'] > leave['date']:
return False
return True

Evaluation : When you create/edit the project system will go to verify this constraint in which the rule is project start date must not be greater then project end date. That will be tested in _check_dates function. If that function will return False then warning will be displayed which will not let user to save entire record till you not input right values. If this function return true then no message will be displayed to end user._constraints will work within python code. So every restriction will be calculated from python function. You will get the current record's ID within function, so you can browse whole record and check your preferred condition.

I hope this helps,

2 Comments
Karthik Arumugam India
7/13/15, 9:44 AM

Thanks for helping me

Emipro Technologies Pvt. Ltd. India
7/13/15, 9:58 AM

Hope this answer made your understanding clear for _constraints.

4
Avatar

Solanki Shamji India

--Solanki Shamji--

1366
| 5 2 9
Baroda, India
--Solanki Shamji--

Hello!

Solanki Shamji India
7/13/15, 8:19 AM

Here is the simple python constraint example which will help you to set constraint :

_columns = {

    'age' : fields.float('Age')

}

def _check_age(self, cr, uid, ids, context=None):

    obj = self.browse(cr, uid, ids[0], context=context)

    if obj.age <= 20:

        return False

    return True

_constraints = [ (_check_age, 'Age must be greater than 21', ['age']), ]

1 Comment
Karthik Arumugam India
7/13/15, 9:44 AM

Its nice , thanks for helping me

0
Rachid Morocco
12/23/16, 4:43 PM

For versions >= 8.0


from openerp.exceptions import  ValidationError

@api.one

@api.constrains('age')

def _check_age(self)

       if self.age<=20:

         raise ValidationError('Age must be greater than 21!")


Ask a Question
Writer
Keep Informed
1 follower(s)
About This Community

This platform 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.

Register
Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now