Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
2001 Vistas

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.

Avatar
Descartar
Autor Mejor respuesta

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


Avatar
Descartar
Mejor respuesta

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

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
0
sept 24
1250
2
ago 25
847
1
mar 25
1221
0
nov 24
1170
1
sept 24
3948