コンテンツへスキップ
メニュー
この質問にフラグが付けられました
2 返信
8338 ビュー

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
2905
0
11月 18
3227
2
11月 23
7447
2
5月 23
7681
2
3月 22
9726