My model: Tenant.
In model Tenant I have field phone, I want when I create a new tenant (In case of creating a new account first (EX: login = "0123 456 789") , then create tenant), field "phone" in this model will link with field "login" in model res_users.
On the contrary, in case of creating a new tenant first (Ex: 0123 456 789), when I create account in res_users, "login" in this model auto link with "phone" in tenant.
Model Tenant:
class Tenant(models.Model):
_name = "pma.tenant"
_inherit = ["mail.thread", "mail.activity.mixin"]
_description = "Manager Tenant"
_order = "create_date desc"
name = fields.Char(string="Tenant Name", required=True)
owner = fields.Many2one("res.users", string="Owner", required=True, default=lambda self: self.env.user)
phone = fields.Char(string="Phone Number", required=True)
user = fields.Many2one("res.users")
email = fields.Char(string="Email Address", required=True)
@api.onchange("phone")
def _compute_check_phone_number(self):
for record in self:
convert_phone = utils.convert_phone_to_global(self.phone)
if convert_phone:
record.user = self.env["res.users"].search( [("login", "=", convert_phone)])
Model Res_users:
class Users(models.Model):
_inherit = "res.users"
@api.model_create_multi
def create(self, vals_list):
users = super(Users, self).create(vals_list)
for user in users:
convert_login = utils.convert_phone_to_vn(user.login) if convert_login:
tenant = (self.env["pma.tenant"].sudo().search([("phone", "=", convert_login)]))
tenant.write({"user": users.id})
return users
I have linked, but it only works in 1 case: creating a new Previous Tenant, creating an account will automatically link.
I want it works for bolh case.
Please help me ....
https://docs.google.com/document/d/1S2yev5sgDE86gu4ut2muT0aHSSMOMc8tv3ciYjJZCkI/edit