Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
4209 มุมมอง

hi all, iam a newbie to odoo iam trying to call a fuction when a boolean field is clicked 

this is my field 

x = fields.Boolean (string = "SAM", default = False)



this is my function, this work fine without the button click condition but when i tried with this conndition i get no error but its not calling the function ie its not working

  1.     @ api.model

  2.     def x (self, vals):

  3.         rec = super (SaleOrder, self) .create (vals)

  4.         if self.transit_check:

  5.             rec = super (SaleOrder, self) .create (vals)

  6.             product_id = self.env ['product.product']. search ([('default_code', '=', 'XX')])

  7.             if product_id:

  8.                 y = 0

  9.                 for each in rec.order_line:

  10.                     y + = each.y +2

  11.                  self.env ['sale.order.line']. create ({'product_id': product_id.id, 'price_unit': y, 'order_id': rec.id, 'product_uom_qty': 1,})

  12.             return rec

  13.         else:

  14.             return rec


someone help me

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

There are lots of ways to do this. Like on save, on button click, or may be onchange. I think you want to do it with onchange of the boolean field.

Try the following code:

@api.onchange('x')
def onchange_x(self):
if self.x:
# Find the product
product_id = self.env ['product.product']. search ([('default_code', '=', 'XX')]).id
# Create a new line with the product
self.order_line += self.env['sale.order.line'].new({'product_id': product_id.id, 'price_unit': 10, 'product_uom_qty': 1,})
return {}



อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hello,

As far as I understand onchange events can only return values, warnings or domains (as a dictionary). 

The create method returns record/records which have been created. 

Thanks, 


EDIT: 

I have read your code wrong, I read @api.model as @api.onchange, the condition is not calling because it looks like there is no @api.onchange but even so you cannot return the records at an onchange event. 

My suggestion would be to override the create method, so change the function eg:

# look in the models.py file to find specific decorates for your Odoo

def create(self,values):

    # call super

    rec = super (SaleOrder, self) .create (vals)

    # if the tickbox is checked

    if x:

        # run your code

    

    return rec


I am unsure how to specifically do it for your Odoo, but this is the idea. Once the user saves the document, if x has been ticked then run your code, otherwise do nothing.


Would this work?


อวตาร
ละทิ้ง
ผู้เขียน

so what can i give here create method ?

I don't think what you are trying to do is possible.

What is your end goal and then maybe there would be another way to achieve what you are after?

Thanks,

ผู้เขียน

when i button is clicked iam trying a add a product in the sale order line automatically , now when i create a sale order it will add without button click condition , but with this condition iam unable to add the product(service kind of thing in the line) but no errors so far.

ผู้เขียน

i have manually added a product (service) , i need to add that in the order line based on button click

I have amended my answer to try and provide a solution

ผู้เขียน

no its not working but still thanks for the answer , the function gives me no error but its not doing anything

Related Posts ตอบกลับ มุมมอง กิจกรรม
How to track One2many field in Odoo12? แก้ไขแล้ว
3
พ.ย. 23
17487
Error: Couldn't load module web แก้ไขแล้ว
3
พ.ย. 24
25134
How I get the current ID of a record ? แก้ไขแล้ว
1
เม.ย. 23
6185
2
ธ.ค. 22
6935
1
พ.ย. 22
4077