Skip to Content
Menu
This question has been flagged
1 Reply
5875 Views

Hi all,

Can anyone take a look at my code I got below error message, when adding 2 lines on my "onchange_prodcode" funchtion. please see the lines with bold once in onchange.

is there any wrong with my onchange code?

 

Thanks

XML

        <record id="abs_product_description" model="ir.ui.view">
        <field name="name">abs.product.description.form.view</field>
        <field name="model">abs.prod.desc</field>
        <field eval="7" name="priority"/>
        <field name="arch" type="xml">
        <form string="Product" version="7.0">
            <sheet>
                <div class="oe_edit_only">
                    <label for="name" string="Product Code"/>
                </div>
               <h1><field name="name" style="width: 37%%"/></h1>
                <group>
...........

...........
                   <field name="price_id" style="width: 37%%"/>
                </group>
            </sheet>
        </form>    
        </field>
    </record>    

PYTHON

class test_product(osv.Model):
    _name = "test.product"

    _columns = {
        'prodcode_id': fields.many2one("abs.prod.desc", 'Product Code'),
        'description_id': fields.many2one('abs.desc', 'Description'),
        'description': fields.related('description_id', type='many2one', relation='abs.desc', string='Description',),
        'variants_id': fields.many2one('abs.var', 'Variants'),
        'variants': fields.related('variants_id', type='many2one', relation='abs.prod.var', string='Variants',),
    }
    
    def onchange_prodcode(self, cr, uid, ids, prodcode_id, context=None):
        if prodcode_id:
            description_id = self.pool.get('abs.prod.desc').browse(cr, uid, prodcode_id, context).description_id.id
            variants_id = self.pool.get('abs.prod.var').browse(cr, uid, prodcode_id, context).variants_id.id
            return {'value':{'description_id':description_id,'variants_id':variants_id}}
        return {}

class descriptionproduct(osv.osv):
    _description="Description Product"
    _name = 'abs.prod.desc'
    _columns = {
        'description_id': fields.many2one('abs.desc', 'Description', required=True),
        'variants_id': fields.many2one('abs.var', 'Variants', required=True),
        'name': fields.char('Code', size=64, required=True,),
    }
    
class description(osv.osv):
    _name = 'abs.desc'
    _description = 'Description'
    _columns = {
        'name': fields.char('Description Name', size=64, required=True,),
    }


class variantsproduct(osv.osv):
    _description="Variants Product"
    _name = 'abs.prod.var'
    _columns = {
        'variants_id': fields.many2one('abs.var', 'Variants', required=True),
        'name': fields.char('Variants Name', size=64, required=True,),
    }

class variants(osv.osv):
    _name = 'abs.var'
    _description = 'Variants'
    _columns = {
        'name': fields.char('Variants', size=64, required=True,),
    }

 

Avatar
Discard
Author

any body can take a look at this?

Author

please see the screen shots, when hitting save populate only the code and description but cannot fetch the variants value. create-- http://tinypic.com/view.php?pic=v7cgmc&s=8#.VFep_4XzSR9 after saving http://tinypic.com/view.php?pic=23tphx&s=8#.VFep5YXzSR8

Best Answer

    class test_product(osv.Model):
        _name = "test.product"

        ...
            'variants': fields.related('variants_id', type='many2one', relation='abs.var', string='Variants',),
        ...

Model 'abs.var' doesn't contain variants_id field. Maybe what you want is change this: relation='abs.prod.var'.

Avatar
Discard
Author

Thanks so much
what you mean is this?
and where can i add relation='abs.prod.var'.? class variants(osv.osv):
----_name = 'abs.var'
----_description = 'Variants'
----_columns = {
--------'name': fields.char('Variants', size=64, required=True,),
--------'variants_id': fields.many2one('abs.var', 'Variants', required=True),
----}

Author

or replace from
'variants': fields.related('variants_id', type='many2one', relation='abs.var', string='Variants',),
to
'variants': fields.related('variants_id', type='many2one', relation='abs.prod.var', string='Variants',),

Yes, that's what I mean to change the variants field in test.product model.

Author

ok, will try this now

Author

I got no error but the variants field no value

Author

I already update above code, replacing from abs.var to abs.prod.var. no error but the variants field no value

on onchange_prodcode, there are two return, fix it.

Author

Thank you so much is working now, update already the above code
----def onchange_prodcode(self, cr, uid, ids, prodcode_id, context=None):
--------if prodcode_id:
------------description_id = self.pool.get('abs.prod.desc').browse(cr, uid, prodcode_id, context).description_id.id
------------variants_id = self.pool.get('abs.prod.desc').browse(cr, uid, prodcode_id, context).variants_id.id
------------return {'value':{'description_id':description_id,'variants_id':variants_id}}
--------return {}