python - How to store the current logged user id or active user id in odoo 9?

Python - How to store the current logged user id or active user id in odoo 9?

In Odoo 9, you can store the current logged-in user's ID or active user's ID in various ways depending on where and how you want to use this information within your Odoo modules. Here's a straightforward approach to achieve this:

Method 1: Using self.env.user

Odoo provides a self.env.user object within models and controllers that represents the currently logged-in user. You can access various details about the user through this object, including their ID (self.env.user.id). Here's how you can use it:

  1. Within a Model Method:

    from openerp import models, fields, api class YourModel(models.Model): _name = 'your.model' @api.model def your_method(self): current_user_id = self.env.user.id # You can now use current_user_id as needed in your method # For example, store it in a field or perform operations based on it 
  2. Within a Controller (if you are implementing a web controller):

    from openerp import http from openerp.http import request class YourController(http.Controller): @http.route('/your/route', type='http', auth='user') def your_controller_method(self): current_user_id = request.env.user.id # You can now use current_user_id as needed in your controller method # For example, return a response based on the current user 

Method 2: Storing in a Field (Optional)

If you need to store the current user's ID in a specific model field for tracking purposes or other functionalities, you can do so by defining a Many2one field linked to the res.users model:

from openerp import models, fields class YourModel(models.Model): _name = 'your.model' user_id = fields.Many2one('res.users', string='Current User', default=lambda self: self.env.user.id) 

In this example, user_id will store the ID of the current logged-in user by default when a record of YourModel is created.

Important Notes:

  • Ensure that the user has logged in before accessing self.env.user, as it may not be available if the user is not authenticated.
  • Always consider security and access rights when dealing with user data. Odoo manages access control through security rules (ir.model.access.csv) and object-level permissions (_security attribute in models).
  • Odoo 9 is quite outdated, and it's highly recommended to upgrade to a more recent version (like Odoo 14 as of now) for better performance, security, and features.

By using self.env.user.id, you can effectively access and utilize the current logged-in user's ID or active user's ID in your Odoo 9 modules as needed. Adjust the approach based on your specific use case and module requirements.

Examples

  1. Odoo 9 get current logged user ID in model

    Description: Use self.env.uid to get the current logged-in user ID in an Odoo model.

    Code:

    class MyModel(models.Model): _name = 'my.model' @api.model def create(self, vals): vals['user_id'] = self.env.uid return super(MyModel, self).create(vals) 
  2. Odoo 9 get current user in controller

    Description: Access the current logged-in user ID using request.env.user.id in an Odoo controller.

    Code:

    from odoo import http from odoo.http import request class MyController(http.Controller): @http.route('/my/route', type='http', auth='user') def my_route(self): user_id = request.env.user.id return "Current User ID: %s" % user_id 
  3. Odoo 9 store user ID in a custom field

    Description: Store the current logged-in user ID in a custom field when a record is created.

    Code:

    class MyModel(models.Model): _name = 'my.model' user_id = fields.Many2one('res.users', string='User', default=lambda self: self.env.user) @api.model def create(self, vals): vals['user_id'] = self.env.uid return super(MyModel, self).create(vals) 
  4. Odoo 9 get current user ID in view

    Description: Use context in XML to pass the current user ID to a view.

    Code:

    <record id="view_my_model_form" model="ir.ui.view"> <field name="name">my.model.form</field> <field name="model">my.model</field> <field name="arch" type="xml"> <form> <field name="user_id" context="{'default_user_id': uid}"/> </form> </field> </record> 
  5. Odoo 9 store active user ID in wizard

    Description: Store the current logged-in user ID in a wizard model.

    Code:

    class MyWizard(models.TransientModel): _name = 'my.wizard' user_id = fields.Many2one('res.users', string='User', default=lambda self: self.env.user) 
  6. Odoo 9 set user ID on write

    Description: Update the user ID field when a record is modified.

    Code:

    class MyModel(models.Model): _name = 'my.model' user_id = fields.Many2one('res.users', string='User') @api.multi def write(self, vals): vals['user_id'] = self.env.uid return super(MyModel, self).write(vals) 
  7. Odoo 9 log current user ID

    Description: Log the current user ID for debugging purposes.

    Code:

    import logging _logger = logging.getLogger(__name__) class MyModel(models.Model): _name = 'my.model' @api.model def create(self, vals): _logger.info("Current User ID: %s", self.env.uid) return super(MyModel, self).create(vals) 
  8. Odoo 9 use user ID in automated actions

    Description: Use the current user ID in an automated server action.

    Code:

    class MyModel(models.Model): _name = 'my.model' user_id = fields.Many2one('res.users', string='User') @api.model def automated_action(self): self.write({'user_id': self.env.uid}) 
  9. Odoo 9 pass user ID in context

    Description: Pass the current user ID in the context when creating a record.

    Code:

    class MyModel(models.Model): _name = 'my.model' @api.model def create(self, vals): ctx = dict(self.env.context, user_id=self.env.uid) return super(MyModel, self.with_context(ctx)).create(vals) 
  10. Odoo 9 use current user ID in domain

    Description: Filter records based on the current user ID in a domain.

    Code:

    class MyModel(models.Model): _name = 'my.model' @api.model def get_user_records(self): return self.search([('user_id', '=', self.env.uid)]) 

More Tags

nem tabpage angular-ngmodel stub spring-mybatis spark-excel dynamic-variables python-2.x lowercase wolfram-mathematica

More Programming Questions

More Various Measurements Units Calculators

More Chemistry Calculators

More Mixtures and solutions Calculators

More Entertainment Anecdotes Calculators