law rence
law rence

Reputation: 179

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

result

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

Upvotes: 1

Views: 547

Answers (1)

Bhoomi Vaishnani
Bhoomi Vaishnani

Reputation: 718

  • 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

Upvotes: 1

Related Questions