Skip to Content
Menu
This question has been flagged
16 Replies
16926 Views

I was trying to create a new company when a record is created in my module but using the create method 

but I can't get it to work

     @api.model
def create(self, vals):
for n_record in self:
company_info ={
'company_type': 'company',
'name': n_record.new_company_name,
'vat': n_record.company_trn
}
record = n_record.env['base.view_partner_form'].create(company_info)

return record

I got this error and I can't understand the Error 

Odoo Server Error
Traceback (most recent call last):
........
........
 return result.id if isinstance(args[0], Mapping) else result.ids
AttributeError: 'NoneType' object has no attribute 'id'

can please help 


edit

@api.model
def create(self, vals):
company_info ={
'company_type': 'company',
'name': self.new_company_name,
'vat': self.company_vat
}
record = self.env['res.partner'].create(company_info)

return record
Avatar
Discard

Override odoo methods: https://goo.gl/4BkizH

Best Answer
Hi,
Remove return record
and add return super "yourclassname"
@api.model
def create(self, vals):
company_info ={
'company_type': 'company',
'name': self.new_company_name,
'vat': self.company_vat
}
record = self.env['res.partner'].create(company_info)
return super(yourclassname, self).create(vals)


Avatar
Discard
Author

can you explain using my code, I can't understand your example

Author

what is the `ClassName` for `res.partner` or the contacts

Are you overriding default create method? In that case create method add below code: change "classname" into your class name.

return super(classname, self).create(values)

Author

I am overriding my module `A` create method so after creating the recored in `A` it create a recored in odoo contacts module `B`

Updated code, please check remove return record and add return super yourclassname ...

Author

how to make it run a function on create instead of having the code in the create function it self

Best Answer

Hi,

Change this line record = n_record.env['base.view_partner_form'].create(company_info) to

record = self.env['res.company'].create(company_info) and see .


For creating records from code, see : Create Record From Code in Odoo


For Over riding the create method,

@api.model
def create(self, vals):
result = super(ClassName, self).create(vals)
# do what you want
return result

See : - How To Override Create Function in Odoo


Thanks

Avatar
Discard
Author

That is the tutorial that I was following

I am still getting the error

File "/usr/lib/python3/dist-packages/odoo/api.py", line 385, in call_kw

result = _call_kw_model_create(method, model, args, kwargs)

File "/usr/lib/python3/dist-packages/odoo/api.py", line 366, in _call_kw_model_create

return result.id if isinstance(args[0], Mapping) else result.ids

AttributeError: 'NoneType' object has no attribute 'id'

Author

I user this instead

@api.model

def create(self, vals):

company_info ={

# 'company_type': 'company',

'name': self.new_company_name,

'vat': self.company_trn

}

record = self.env['res.company'].create(company_info)

return record

and geting

×

The operation cannot be completed:

- Create/update: a mandatory field is not set.

- Delete: another model requires the record being deleted. If possible, archive it instead.

Model: Companies (res.company), Field: Company Name (name)

Make sure that the value for the all the mandatory fields are passed in to the create method, here in your code as per the error self.new_company_name might be returning empty or False, just print it and see, also you can try with vals['new_company_name']

Thanks

Author

I tried vals['new_company_name'] I am not going the error but is stuck save

Author

the company has been created but the form is not saving

Author

how to make it run a function on create instead of having the code in the create function it self

Related Posts Replies Views Activity
2
Jul 21
5305
4
May 24
12205
1
Apr 24
2933
0
Nov 23
1805
1
Sep 23
1835