{"id":773,"date":"2021-06-14T10:59:10","date_gmt":"2021-06-14T04:59:10","guid":{"rendered":"https:\/\/mellowhost.com\/blog\/?p=773"},"modified":"2021-06-15T15:35:32","modified_gmt":"2021-06-15T09:35:32","slug":"how-to-add-fields-to-res-users-or-res-partners-model-in-odoo","status":"publish","type":"post","link":"https:\/\/mellowhost.com\/blog\/how-to-add-fields-to-res-users-or-res-partners-model-in-odoo.html","title":{"rendered":"How to Add fields to res.users or res.partners model in Odoo"},"content":{"rendered":"\n<p>res.users and res.partners tables are two base tables of odoo. If you would like to inherit and extend them, remember, you can&#8217;t do it from the Odoo user view. The reason is, when you do an upgrade from the user view, it has to be something that works over base module, not base module itself. Hence, you will get a 500 error or internal server error when trying to upgrade the module. <\/p>\n\n\n\n<p>We will make a simple module for res.users to extend the model to add a field called &#8216;access_token&#8217; for each user, and generate a key automatically when a user is added. <\/p>\n\n\n\n<p>I will only post the model file and the view file here. I expect you already know how to write an Odoo module.<\/p>\n\n\n\n<p>This is my res_users.py file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from odoo import api, fields, models\nimport string, random\n\nclass res_users_ex(models.Model):\n    _inherit = 'res.users'\n\n    access_token = fields.Text(string='API Access Token', default=False)\n\n    def create(self, vals):\n        key = ''.join(random.choices(string.ascii_lowercase + string.digits, k = 48))\n        user_id = super(res_users_ex, self).create(vals)\n        if key:\n            user_id.access_token = key\n\n        return user_id\n<\/pre>\n\n\n\n<p>Here is the xml view file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;odoo>\n    &lt;record model=\"ir.ui.view\" id=\"view_res_users_access_token\">\n        &lt;field name=\"name\">res.users.add.access.token&lt;\/field>\n        &lt;field name=\"model\">res.users&lt;\/field>\n        &lt;field name=\"inherit_id\" ref=\"base.view_users_form\"\/>\n        &lt;field name=\"type\">form&lt;\/field>\n        &lt;field name=\"arch\" type=\"xml\">\n        &lt;xpath expr=\"\/\/notebook\" position=\"inside\">\n            &lt;page string=\"API Details\">\n               &lt;group col=\"4\">\n                  &lt;field name=\"access_token\"\/>\n                &lt;\/group>\n             &lt;\/page>\n        &lt;\/xpath>\n        &lt;\/field>\n    &lt;\/record>\n&lt;\/odoo><\/pre>\n\n\n\n<p>Now, after you have added this to a module, you can not simply upgrade this from App &gt;&gt; Module &gt;&gt; Upgrade. You need to upgrade this module via command line like the following:<\/p>\n\n\n\n<p>First switch to your odoo user, in my case, it is &#8216;odoo&#8217;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">su - odoo<\/pre>\n\n\n\n<p>Now, first stop your current odoo with the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">service odoo14 stop<\/pre>\n\n\n\n<p>Once done, now you can upgrade the module with the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/usr\/bin\/scl enable rh-python36 -- \/opt\/odoo\/odoo14-venv\/bin\/python3 \/opt\/odoo\/odoo14\/odoo-bin -d my_database-u res_users_access_token<\/pre>\n\n\n\n<p>The command above is explained like the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python3 odoo-bin -d your_databasename -u module_name<\/pre>\n\n\n\n<p>In my case, I use virtual environment and scl for python, hence the python3 source is like the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/usr\/bin\/scl enable rh-python36 -- \/opt\/odoo\/odoo14-venv\/bin\/python3<\/pre>\n\n\n\n<p>The next one is the binary of odoo with it&#8217;s location, which should be odoo-bin. With the parameter -d, you give your database name, and with the parameter -u, you need to give your module name. After you run the command, you should see no &#8216;Error&#8217; or Red marked line in your console. If not, it shall be upgraded. Now do control + c, and start your odoo again to see the new fields being visible in your Users tab. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>res.users and res.partners tables are two base tables of odoo. If you would like to inherit and extend them, remember, you can&#8217;t do it from the Odoo user view. The reason is, when you do an upgrade from the user view, it has to be something that works over base module, not base module itself. &hellip; <a href=\"https:\/\/mellowhost.com\/blog\/how-to-add-fields-to-res-users-or-res-partners-model-in-odoo.html\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Add fields to res.users or res.partners model in Odoo&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[292,271],"tags":[571,573,297,572],"_links":{"self":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/773"}],"collection":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/comments?post=773"}],"version-history":[{"count":2,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/773\/revisions"}],"predecessor-version":[{"id":775,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/773\/revisions\/775"}],"wp:attachment":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/media?parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/categories?post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/tags?post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}