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
- Accounting
- Inventory
- PoS
- Project
- MRP
This question has been flagged
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
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign upRelated Posts | Replies | Views | Activity | |
---|---|---|---|---|
|
0
Sep 24
|
1277 | ||
|
2
Aug 25
|
895 | ||
|
1
Mar 25
|
1311 | ||
|
0
Nov 24
|
1217 | ||
|
1
Sep 24
|
4067 |