콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
12487 화면

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.

아바타
취소
베스트 답변

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,,,

베스트 답변

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

Thanks, Priyesh Solanki

아바타
취소
관련 게시물 답글 화면 활동
1
4월 23
4299
1
12월 19
5109
0
3월 15
4140
0
1월 25
1708
0
11월 24
1113