所以我有3个类别。首先是没有孩子,第二是总是有孩子,但有时没有父母,有时 第三类总是没有父母。
我的目标是从下到上遍历这3个类别。
首先,我需要检查没有孩子的类别,其次是secodn类别,然后是第三。
如果满足我的条件,我只是显示消息,如果不符合条件,我转到另一个类别并检查条件。
所以我写了这段代码,还可以,但是也许我可以不用重复自己就可以做,并使我的代码更简单?
msg = _("some %s %s message: %s.")
msgs = []
for line in order.order_line:
parent_id = line.product_id.categ_id.parent_id
parent_parent = line.product_id.categ_id.parent_id
categ_id = line.product_id.categ_id
categorys = parent_id + parent_parent + categ_id
categorys = parent_id + parent_parent + categ_id
for categ in categorys:
if not categ.childs_id and categ.qty_for_discount:
if line.product_qty < categ.qty_for_discount:
msgs.append(
msg % (
categ.qty_for_discount - line.product_qty,
line.product_id.uom_id.name,
categ.name
)
)
elif categ.parent_id and categ.child_id and categ.qty_for_discount:
if line.product_qty < categ.qty_for_discount:
msgs.append(
msg % (
categ.qty_for_discount - line.product_qty,
line.product_id.uom_id.name,
categ.name
)
)
else:
if line.product_qty < categ.qty_for_discount:
msgs.append(
msg % (
categ.qty_for_discount - line.product_qty,
line.product_id.uom_id.name,
categ.name
)
)
答案 0 :(得分:1)
利用Python的可变范围。尝试这样的事情:
def your_method(self):
msgs = []
def append_msg():
msgs.append( _("some %s %s message: %s.")% (
categ.qty_for_discount - line.product_qty,
line.product_id.uom_id.name,
categ.name
))
for line in order.order_line:
parent_id = line.product_id.categ_id.parent_id
parent_parent = line.product_id.categ_id.parent_id
categ_id = line.product_id.categ_id
categories = parent_id + parent_parent + categ_id
categories = parent_id + parent_parent + categ_id
for categ in categories:
if not categ.childs_id and categ.qty_for_discount:
if line.product_qty < categ.qty_for_discount:
append_msg()
elif categ.parent_id and categ.child_id and categ.qty_for_discount:
if line.product_qty < categ.qty_for_discount:
append_msg()
else:
if line.product_qty < categ.qty_for_discount:
append_msg()