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

Python constraints (error) message.

By
supreeth
on 9/21/15, 1:38 AM 720 views


Link: http://stackoverflow.com/questions/32688253/python-constraints-error-message

I have written a function like below in Python. works like if user selects holiday start-date and holiday end-date with time correctly it will accepts, else it will show an error message like 'Error: Entered Invalid Date or Time'


Python code:

def _date_start_end_validate(self, cr, uid, ids, context=None):
sr_ids = self.search(cr, 1 ,[], context=context)
for self_obj in self.browse(cr, uid, ids, context=context):
if self_obj.date_start >= self_obj.date_end:
return False
return True
_constraints = [(_date_start_end_validate, 'Error: Entered Invalid Date or Time', ['Dates'])]

I need to display which start-date is showing error here, that date should to displayed.

How to get that start-date information in constraints.

1

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 9/21/15, 2:54 AM

Supreeth,

You can raise an exception in place of "return False" statement and show your customized message there only....like:
raise osv.except_osv(_('Error:'),_('Entered Invalid Date or Time %s')%(YOUR_DATE_FIELD))

Regards

@Pawan Thanks, It is working I defined: for self_obj in self.browse(cr, uid, ids, context=context): xcv = self_obj['description'] if self_obj.date_start >= self_obj.date_end: raise osv.except_osv(_('Error:'),_('Entered Invalid Date/Time: %s')%(xcv)) return True Works fine.

supreeth
on 9/21/15, 4:33 AM

Great! :)

Pawan
on 9/21/15, 5:13 AM
1

Ankit H Gandhi(AHG)

--Ankit H Gandhi(AHG)--
996
| 5 3 6
Rajkot, India
--Ankit H Gandhi(AHG)--

Learning Python..

Ankit H Gandhi(AHG)
On 9/21/15, 4:02 AM

Dear supreeth,

Try below code may be use full.

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

    sr_ids = self.search(cr, 1 ,[], context=context)

    for self_obj in self.browse(cr, uid, ids, context=context):

        if self_obj.date_start >= self_obj.date_end:

            return False

    return True

def _check_msg_with_start_date(self, cr, uid, ids, context):

    data = self.browse(cr, uid, ids, context=context)

    return "Error: Entered Invalid Date or Time {} ".format(data.date_start)

_constraints = [

    (_date_start_end_validate, lambda self, *a, **kw: self._check_msg_with_start_date(*a, **kw), ['date_start','date_end'])

]

Thanks & Regards

Ankit H Gandhi

Thanks @ankit

supreeth
on 9/21/15, 4:44 AM
1

Solanki Shamji

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

Hello!

Solanki Shamji
On 9/21/15, 2:51 AM

def _date_start_end_validate(self, cr, uid, ids, context=None):
self_data = self.browse(cr, uid, ids[0], context=context) 
if self_data.date_start and if self_data.date_end:
start_date = datetime.strptime(self_data.date_star, DEFAULT_SERVER_DATETIME_FORMAT)
end_date = datetime.strptime(self_data.date_end, DEFAULT_SERVER_DATETIME_FORMAT)
if start_date > end_date:
return False
else:
return True
return True

_constraints = [
(_date_start_end_validate, 'Error!\nEntered Invalid Date or Time', ['date_start','date_end'])
]

Try above code may be this will help you.

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: 9/21/15, 1:38 AM
Seen: 720 times
Last updated: 9/21/15, 4:02 AM