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.

2

How do I set python constraints on Odoo/OpenERP 7.0 ?

By
Karthik Arumugam
on 7/13/15, 7:53 AM 1,173 views

Hi friends can any body help me to set python constraints on openerp 7.0 with an example program

6

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 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,

Thanks for helping me

Karthik Arumugam
on 7/13/15, 9:44 AM

Hope this answer made your understanding clear for _constraints.

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

Solanki Shamji

--Solanki Shamji--
1293
| 5 2 6
Baroda, India
--Solanki Shamji--

Hello!

Solanki Shamji
On 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']), ]

Its nice , thanks for helping me

Karthik Arumugam
on 7/13/15, 9:44 AM

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: 7/13/15, 7:53 AM
Seen: 1173 times
Last updated: 8/31/15, 10:31 AM