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
- Księgowość
- Zapasy
- PoS
- Project
- MRP
To pytanie dostało ostrzeżenie
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
Podoba Ci się ta dyskusja? Dołącz do niej!
Stwórz konto dzisiaj, aby cieszyć się ekskluzywnymi funkcjami i wchodzić w interakcje z naszą wspaniałą społecznością!
Zarejestruj sięPowiązane posty | Odpowiedzi | Widoki | Czynność | |
---|---|---|---|---|
|
0
wrz 24
|
1459 | ||
|
2
sie 25
|
1250 | ||
|
1
mar 25
|
1740 | ||
|
0
lis 24
|
1448 | ||
|
1
wrz 24
|
4479 |