I want to create a new company when I create a new account, I have written a code for that, I used res.users but can't get to the core of the problem.
I have created a new model for managing company and made many2one field with res.users, but still my desired thing is not being done.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Boekhouding
- Voorraad
- PoS
- Project
- MRP
Deze vraag is gerapporteerd
I have used this code
from odoo import models, fields, api, _
class ResUsers(models.Model):
_inherit = 'res.users'
has_logged_in = fields.Boolean(string='Has Logged In', default=False)
company_id = fields.Many2one('my.company', string='Company')
@api.model
def _login(self, db, login, password):
# Call the original login method
res = super(ResUsers, self)._login(db, login, password)
# Check if the user has logged in before
user = self.env['res.users'].sudo().search([('login', '=', login)])
if user and not user.has_logged_in:
# Generate a unique company name using a sequence
company_name = 'Company %s' % self.env['ir.sequence'].next_by_code('my.company.sequence')
# Create a new Company
company_data = {
'name': company_name,
}
new_company = self.env['my.company'].sudo().create(company_data)
# Update the user's has logged_in and company_id fields
user.write({'has_logged_in': True, 'company_id': new_company.id})
return res
Hi,
You can use the following code for creating a new company upon the creation of an account.
class Account(models.Model):
_inherit = 'account.account'
@api.model
def create(self, vals):
res = super().create(vals)
company = self.env['res.company'].sudo().create({
'name': 'New Company'
})Replace your name with your desired name of the company
Hope it helps
Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!
Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!
AanmeldenGerelateerde posts | Antwoorden | Weergaven | Activiteit | |
---|---|---|---|---|
|
0
sep. 24
|
1540 | ||
|
2
aug. 25
|
1414 | ||
|
1
mrt. 25
|
1925 | ||
|
0
nov. 24
|
1529 | ||
|
1
sep. 24
|
4630 |