Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to use copy-method properly?

By
Anders Wallenquist
on 11/25/14, 4:29 AM 2,008 views

I'm trying to copy a company and give it a new name:

company = template_user.company_id.copy(default={'name': 'New name',})

I got this error message:

ValueError: "duplicate key value violates unique constraint "res_company_name_uniq"
DETAIL:  Key (name)=(YourCompany) already exists.

YourCompany are the name of the template_users company.

 

1

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 11/25/14, 4:41 AM

I wonder why a browse object has a copy method.

You need to call the copy mehtod of the company object.

What you might want to try is something like this:

new_company_id = self.pool.get('res_company').copy(cr, uid, YourCompany_id, default={'name': 'New name'}, context=context)

where YourCompany_id could be template_user.company_id.id

 

Regards.
 

Thank you, I have tested self.env['res.company'].copy (I dont have cr/uid) it looks like I got a copy but not any company-object nor company_id returned. def add_activity(self): template_user = self.env['res.users'].browse(literal_eval(self.env['ir.config_parameter'].get_param('auth_signup.template_user_id', 'False')),) assert template_user and template_user.exists(), 'Signup: invalid template user' for user in self: if user.company_id.id == template_user.company_id.id: company_id = self.env['res.company'].copy(template_user.company_id.id,default={ 'name': _('%s Activity') % user.name, 'currency_id': template_user.company_id.currency_id.id, 'country_id': template_user.company_id.country_id.id, 'parent_id': template_user.company_id.parent_id.id,}) _logger.warning("res.user company %s" % (company_id)) user.write({'company_ids': (6,_,[company_id,template_user.parent_id.id]),'company_id': company_id})

Anders Wallenquist
on 12/2/14, 5:32 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/25/14, 4:29 AM
Seen: 2008 times
Last updated: 5/3/15, 12:51 PM