-1

I have a model named Joked and I wish to store the email of the current user as foreign key. In my model I have defined my M2O relationship as shown:

last_accessed_by = fields.Many2one('res.users', string='Complainant id', default=lambda self: self.env.uid, required = True)

However, it takes the id of the user and not the email.

Mingg Lex
  • 151
  • 3
  • 12
  • You are explicitly telling it to store a `res.users` object and defaulting it to the user ID. Can you better explain what you are wanting and possibly why? It seems like a strange use case. Typically, you would store the user ID and display any related fields you need. – travisw Apr 29 '18 at 14:23
  • 1
    This is because I require sending email to the user. And using the automated action for emails, it just fetches the user's email from the database using `${object.last_accessed_by}` to send to that user. I am having difficulty in getting the user's email if i use the user id as foreign key. Do you have any idea how to get the user's email while i use the user id as foreign key @travisw? – Mingg Lex Apr 30 '18 at 04:20

1 Answers1

0

In the email template, you can continue using dot notation to access a sub-object’s attributes.

In your case, when you get object.last_accessed_by, you are getting a res.users object. You can continue accessing attributes until you get to what you need.

Additionally, all models in Odoo have a few “builtin” fields. One of those is `write_uid, which stores what user last modified the record.

I would suggest using it instead of your last_accessed_by field since the seem to be identical. You could end up with something like this:

object.write_uid.partner_id.email
travisw
  • 2,052
  • 1
  • 14
  • 27