Skip to Content
Menu
This question has been flagged
1 Reply
6082 Views

want to get currently logged in user id as default value in python file but when submit/save it is showing NULL value when querying thru psql. how i can get it, below are what i did, row saved but without current user's id in field user_id.

in python file:

user_id = fields.Many2one('res.partner', string="User",compute='_get_current_user')
    @api.depends('name')
    def _get_current_user(self):
        for r in self:
            r.user_id = self.env.user

created a web page as my view for data entry and controller file:

class QuickOrdersForm(http.Controller):
    @http.route(['/web/quickorders/form'], type='http', auth="public", website=True)
    def quickorders_form(self, **post):
        return request.render("tests.web_quickorders_form", {})
    @http.route(['/web/quickorders/form/submit'], type='http', auth="public", website=True)
    # next controller with url for submitting data from the form#
    def quickorders_form_submit(self, **post):
        qorder = request.env['tests.quickorders'].create({
            'name': post.get('name'),
            'order_type': post.get('order_type')
        })
        vals = {
            'qorder': qorder,
        }
        # inherited the model to pass the values to the model from the form#
        return request.render("tests.web_quickorders_form_success", vals)


Avatar
Discard
Best Answer

Mr.Smith,

            Your Many2one field user_id  points to the table 'res.partner'. It is wrong and it should be res.users in order to capture the current user who has logged in. 
user_id = fields.Many2one('res.users', string="User", compute='_get_current_user')

Avatar
Discard
Author

thanks to point my mistake, will change and confirm.

Hope it helps. If so please close your query as answered. Thanks

Author

@Karthikeyan, i modified it as per instructed but still not getting logged-in user's 'id' in my user_id field. is there anything in function _get_current_user or something else?

You can simply use "default" in your field definition instead of writing a compute,

user_id = fields.Many2one('res.users', string='User', default=lambda self: self.env.user)

Author

yeah, now it works perfectly... modified to use default=lambda self: self.env.user

Related Posts Replies Views Activity
3
Sep 22
2991
0
May 22
1320
0
Mar 22
5029
1
Nov 24
1482
1
Nov 24
1188