Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
2804 Lượt xem

from email.policy importdefault
from attr import fields

from odoo import models, fields, api
classFashionOrder(models.Model):

_name = 'fashion.order'#untuk lead

_rec_name = 'order_name'
customer_name = fields.Many2one('fashion.customer', string="Name")

order_name = fields.Char("Order Name", default="ORDER_")

notes = fields.Char("Notes")

order_line = fields.One2many('fashion.order.lines','order_id', string = "Order Lines")

totalorder_price = fields.Float(string="Total Order Price", readonly=True, store=True, compute="_compute_total_order_price")
state = fields.Selection([ ('lead', 'Lead'), ('ordered', 'Ordered'), ('paid', 'Paid') ], default='lead', string="Status", required=True)

def action_submit_order(self):

self.state = 'ordered'

view_ordered = self.env.ref('foodstore.action_fashion_order').read()[0]

view_ordered['domain'] = [('state', '=', 'ordered')]

view_ordered['target'] = 'main'

invoice_line_value = []

for line in self.order_line:

invoice_line_value.append((0,0, {'product_id': line.product_id.id, }))
order_rec = {'invoice_customer': self.customer_name.customername,'totalinvoice_price': self.totalorder_price,'state': self.state,'invoice_lines': invoice_line_value, }

order = self.env['fashion.invoice'].create(order_rec)
return view_ordered
def action_delete(self):

print('', self.order_name)

self.unlink()

view_orders = self.env.ref('foodstore.action_fashion_order').read()[0]

view_orders['target'] = 'main'
return view_orders
@api.depends('order_line.total_price')

def_compute_total_order_price(self):

current_price = 0

for product in self.order_line:

current_price += product.total_price

self.totalorder_price = current_price


classFashionOrderLines(models.Model):

_name = 'fashion.order.lines'
product_id = fields.Many2one('fashion.product', string="Product Name")

product_qty = fields.Integer(string = "Quantity", default="1")

order_id = fields.Many2one('fashion.order', string="Order ID")
order_code = fields.Char("Order Code", default="ORDER-")

price_id = fields.Float(related='product_id.price', string="Price")

sale_price = fields.Float(related='price_id', string="Sale Price", readonly=False)

total_price = fields.Float("Total Price", store=True, compute="action_calculate_price")

@api.depends('sale_price','product_qty')

def action_calculate_price(self):

total_prices = self.total_price

total_prices = self.sale_price * self.product_qty

self.total_price = total_prices


Here above is my code, when i add 2 product directly, then save it, this error is appear. Any idea how to solve it?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hello Syah,

You haven't used for loop in "action_calculate_price" compute method in fashion.order.line. That is why it is showing an singleton error. Use for loop then it will work properly.

I hope This will help you.

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi, the matter is every model is a list of records, so if you are trying to change some field then you either have to check that one record is edited on a time, or you have to pass on each record, here is the solution for all of your functions in your code:

def action_calculate_price(self):

for record in self:

total_prices = record.total_price

total_prices = record.sale_price * record.product_qty

record.total_price = total_prices

or you just can do:

@api.depends('sale_price','product_qty')

def action_calculate_price(self):

self.ensure_one()

total_prices = self.total_price

total_prices = self.sale_price * self.product_qty

self.total_price = total_prices now you do like I wrote above for all of your functions in your code. I also suggest you to use more (if) , cause you might have issues like there is no price or quantity entered, so you might got error then, to avoid this I recommended you to us( if (exist) do)

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 7 25
445
3
thg 3 25
962
2
thg 1 25
4425
0
thg 4 17
2558
0
thg 3 15
3792