Skip to Content
Menu
This question has been flagged
5 Replies
2636 Views

i am trying to block previous dates but got this error could you please help me

File "/opt/odoo/odoo/models.py", line 5448, in _onchange_eval
    method_res = getattr(self._origin, method)(*params)
TypeError: onchange_date() takes at least 5 arguments (2 given)

py file:

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

xml file:

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

Avatar
Discard

A function can have as many parameters as you want, but the more parameters you have, the harder it is to memorize their roles and purposes.

This also means that invoking the function will require 5 arguments.

Best Answer

Hi,

You try with this code, Am not sure about it will solve your issue, because in your question, there is no details about the method and version.    

.py

     @api.onchange('payment_date')
     def onchange_date(self):
                   if datetime.strptime(self.payment_date, DEFAULT_SERVER_DATE_FORMAT).date() > datetime.now().date():                   
raise ValidationError(_("Your MSG"))
.xml
no need of any change.


You can use another method too,

    @api.multi
    def write(self, vals):     if vals.get('payment_date'):
            if datetime.strptime(vals['payment_date'], DEFAULT_SERVER_DATE_FORMAT).date() > datetime.now().date():
                raise ValidationError(_('Your Message'))
        res = super(YourClass, self).write(vals) return res


Regards,

Nikhilkrishnan

Avatar
Discard
Author

okay thank you nikhil its working but i want to show block dates wont raise error .how can i do it

onchange method is using only the time of you change the record date.

@api.model

def create(self, vals):

if vals.get('payment_date'):

if datetime.strptime(vals['payment_date'], DEFAULT_SERVER_DATE_FORMAT).date() > datetime.now().date():

raise ValidationError(_('Your Message'))

res = super(YourClass, self).create(vals)

return res

Author Best Answer

i am using odoo 10 ibrahim

Avatar
Discard

Then You are useing the old method, please try with my answer.

Well, you need to update your code with the new api as nikhil showed.

Best Answer

Hi, 

Your function takes 5 arguments which are : cr, uid, ids, payment_date and context. But in your XML, you only give payment_date ( context is set to None by default ).  You need to pass the other arguments as well.

I don't know in which version you're working on, is onchange_date a custom method ?

Avatar
Discard
Author

tq ibrahim

Best Answer

@api.onchange('payment_date')

def onchange_date(self):

        if datetime.strptime(self.payment_date, DEFAULT_SERVER_DATE_FORMAT).date() > datetime.now().date():

            return False

        return self.payment_date

Avatar
Discard