跳至內容
選單
此問題已被標幟
2 回覆
8077 瀏覽次數

I'm working on the project.task  module. I extended the model with a datetime field and extended the view so that I set the attribute required to true. Now I need a to fill this fields with a button so the user can "take" the task. 

But when the button is pressed I get the red popup "The following fields are invalid".

def take_over_task(self):
        uid = self._context.get('uid')
        now = datetime.now(pytz.timezone('Europe/Ljubljana'))

        self.sudo().task_start = datetime.strftime(now, DEFAULT_SERVER_DATETIME_FORMAT)
        self.sudo().date_deadline = datetime.strftime((now + timedelta(days=1)), DEFAULT_SERVER_DATETIME_FORMAT)
        self.sudo().user_id = uid

I know I could just execute a SQL and write this fields directly into the DB, but would like to do this a better correct way.



 

頭像
捨棄
最佳答案

The point is that when you push any Odoo button, before the actual method is executed the current record is saved (either 'created' or 'written'). It means, that check for 'required' and being filled is made before you fill the value in your method.

Basically, I can't see a reason to make a field required in case you fill it with the button (not manually). But if it is really needed, I would suggest the following:

  1. Make a field not required, but required under conditions. E.g. attrs="{'required': [('some_field', '=', True)]}"

  2. Make 'some_field' being True right in the button body. Thus, checks in write and create would be passed correctly until the button is actually pressed.

頭像
捨棄
作者

hm... I don' exactly understand what you're saying. If I make the filed True with the button won't that return the same result? The problem is that I need to allow manually filling and with a quick button. Maybe I'll just make the fields not required and then fill them with default values.

作者

It would probably work your way but I don't have time to figure out how. So I just removed the required attribute and check if the fields are set when the task gets to a certain stage with the api.constrains.

最佳答案

Also you can use this module, that allow bypass required fields validation using special context

https://apps.odoo.com/apps/modules/10.0/xf_form_button_upgrade/

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
2
12月 19
2545
0
11月 18
3098
2
11月 23
7201
2
5月 23
7374
2
3月 22
9473