This question has been flagged
3 Replies
3446 Views

I want to let the definition of a field be dependant upon the value of another field. Concretely, I want the definition of the field product.standard_price to be dependant upon the value of the boolean field product.puchase_ok in such a way that standard_price is either equal to the value of another field or is defined in the standard way (as in the product-module). I am thinking that the definition of the standard_price field should be determined as a function operating like this (in pseudo-code): 

"If (purchase_ok): standard_price = standard defintion of the field

Else: standard_price = foo"

where "foo" is another field in the model.

Is my thinking correct and if so, how can such a function be implemented? (I know how to let a field be defined as a function -- I am more in doubt about the actual implementation of the function).


EDIT: The server is running v7.

Avatar
Discard

your server is v7 or v8

Best Answer

in v8 maybe you'll find useful to override the "_set_standard_price" function of "product.template" model?

for v7 you can try to turn the "standard_price" field into the computed field but make sure you implement both fnct and fnct_inv (also store=True is recommended, it'll preserve old values if any)...

class product_template(osv.osv):
    _name = "product.template"
    _inherit = "product.template"
    def _set_standard_price(self, cr, uid, ids, field_name, field_value, arg, context):
        # set value of standard_price field
    def _get_standard_price(self, cr, uid, ids, field_name, arg, context):
        # get value of standard_price field
    _columns = {
        'standard_price': fields.function(
            _get_standard_price,
            fnct_inv = _set_standard_price,
            method = True,
            store = True, type = 'float',
            digits_compute = dp.get_precision('Product Price'),
            help = "same....", string = 'Cost',
            groups = "base.group_user"),
    }

Avatar
Discard
Author

Thank you for your reply, but I am in doubt as to how to implement the function more concretely.

https://doc.odoo.com/6.0/developer/2_5_Objects_Fields_Methods/field_type/#functional-fields

also don't forget to add "product" module to dependency list in __openerp__.py manifest

Author Best Answer

Sorry, server is v7.

Avatar
Discard