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

Prevent a user from selecting a future date using datepicker?

By
CJ
on 4/2/13, 5:40 PM 2,985 views

I would like to prevent a user from selecting a future date using the date picker. Is this possible? I am using version 6.0.4.

1
Mike Telahun
On 4/3/13, 3:29 AM

Yes, you can create your own module that:

  1. Adds an on_change attribute to the field in the view
  2. Creates a method that gets called by the on_change attribute in #1. The method returns False if the date chosen is greater than today.

For example, in your form view you would have:

<field name="my_date" on_change="onchange_date(my_date)"/>

Then in your object you would put:

from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT

def onchange_date(self, cr, uid, ids, my_date, context=None):
    if datetime.strptime(my_date, DEFAULT_SERVER_DATE_FORMAT).date() > datetime.now().date():
        return False
    return my_date

Edit: add example of constraint mentioned by Priyesh:

A constraint has the added benefit that it doesn't depend on the client interface calling the on_change method. It always gets checked when a value is written to the field.

def _check_date(self, cr, uid, ids, context=None):
    obj = self.browse(cr, uid, ids[0], context=context)
    if datetime.strptime(obj.my_date, DEFAULT_SERVER_DATE_FORMAT).date() > datetime.now().date():
        return False
    return True

_constraints = [(_check_date, 'Date is in the future!', [my_date])]

Thanks a lot,,,

Vikram
on 10/30/13, 1:50 AM
1

Priyesh Solanki (pso)

--Priyesh Solanki (pso)--
Tiny ERP Pvt Ltd
http://www.odoo.com
1100
| 2 2 4
--Priyesh Solanki (pso)--
Priyesh Solanki (pso)
On 4/3/13, 4:09 AM

You can also create Openerp Constraint for this. It will trigger when you will try to save the record.

Thanks, Priyesh Solanki

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

2 follower(s)

Stats

Asked: 4/2/13, 5:40 PM
Seen: 2985 times
Last updated: 3/16/15, 8:10 AM