Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

python - Why is my write() method in Odoo not setting the value?

I have inherited some model. I need also to override it's write method.

I've tried this:

@api.multi
 def write(self, vals, context=None):
     res = super(WebSiteSupportTicket, self).write(vals)
     date = datetime.datetime.now()
     if vals['state_id']:
         if vals['state_id'] == 7 or vals['state_id'] == 8:
             vals['closing_date'] = date
     print(vals)
     return res

Where closing_date is a Datetime field.

When I make the change of state_id to the state with id 7 or 8, closing_date is still being null. But I know the code is passing through the if statement because I can see closing_date on the print of vals

First time I run into a problem with write method. Why is happening and how can I get a solution?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You added the closing_date to the values dict after you called super, the closing_date will not be written.

Remove the context argument from the function definition (not needed). You can find an example where they override the invoice write method in account module.

Example:

@api.multi
def write(self, values):
    # Define the closing_date in values before calling super
     if 'state_id' in values and values['state_id'] in (7, 8) :
         values['closing_date'] = datetime.datetime.now()
    return super(WebSiteSupportTicket, self).write(values)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...