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

Hello,

I have the following problem:

I have started work on an OpenERP module used for quality control, and one requirement for it to work is having linked quality parameters. Each parameter record has a field called 'formula' where I should be able to calculate the value for the parameter based on a dependent parameter.

For example, if I have a 'weight' parameter and a 'tolerance' parameter, I would like to have a 'foreign substance' parameter, where I would calculate the values for the 'foreign substance' parameter through the formula field, for instance 'weight'/'tolerance'.

The formula field would contain text that would be executed as Python code.

My question is, how would I indicate to the 'foreign substance' parameter that it should take into account the values of the other two when calculating its values?

Best regards, Stjepan

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

Use a function field, and have it pull the values of those other fields as well as the formula text? You'd have to be able to parse the formula text to replace the variables with the object references. Or, state that the syntax is

obj.weight/obj.tolerance

Once you're in the function calculating the 'foreign substance' field, make sure the object name is obj:

from openerp.tools import safe_eval
objs = self.pool.get('quality.control').browse(cr, uid, ids)
for obj in objs:
    foreign_substance = safe_eval(obj.formula)

There are more functions in safe_eval.py that you probably want to look into, just to make sure that nobody breaks your system with a bad formula.

Ảnh đại diện
Huỷ bỏ
Tác giả

Thanks, I think this is just what I was looking for. Solved

Câu trả lời hay nhất

Take a look at the bill of material model, it implements the recursivity you are talking about

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 10 24
1390
1
thg 11 23
2653
2
thg 6 16
3379
3
thg 1 16
4680
2
thg 10 24
1489