简体   繁体   中英

def create function with many2one fields in odoo 10, python

I would like to define my name depends on my asset tags plus my asset brand but my asset tag and asset brand fields is Many2one and it doesn't give me an error but the output is wrong. It gives me a number, integer instead of a string(which is the tag name plus the brand name). My expected result is for example my asset tag is Computer, then my asset brand is Asus then my expected result in name would be Computer-Asus. Please help. Many thanks.

This is what i do.

@api.model
def create(self,vals):
    if vals['asset_tag']:
        asset_tag = vals['asset_tag']
    else:
        asset_tag = ''

    if vals['asset_brands_id']:
        brand_id = vals['asset_brands_id']
    else:
        brand_id = ''

    name = "{}-{}".format(asset_tag, brand_id)

    vals['name'] = name
    return super(ModifiedAssetAsset, self).create(vals)

then the output is this

结果

It gives me 2-4 on my asset name but my expecting result is Vehicle-Toyota

  • Please try this code it help you
@api.model
def create(self,vals):
    if vals['asset_tag']:
        asset_tag = vals['asset_tag']
    else:
        asset_tag = ''

    if vals['asset_brands_id']:
        brand_id=self.env['asset.brand.model.name'].search([('id','=',vals
                 ['asset_brands_id'])],limit=1).name
    else:
        brand_id = ''
        name = "{}-{}".format(asset_tag, brand_id)
        vals['name'] = name
        return super(ModifiedAssetAsset, self).create(vals)@api.model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM