Oralover
Oralover

Reputation: 93

odoo 14: how i can add additional fields in SignUp like address etc

i want to add some additional fields in odoo signup form/view like address and also wants to know where it is saving signup data (table) so i can reference / track to it for user related activities. please guide the steps i have to done to achieve this. regards

Upvotes: 1

Views: 4624

Answers (2)

Kenly
Kenly

Reputation: 26678

You need to inherit auth_signup.fields template to add the field to the sign-up page. The following code add the street to the signup fields.

<odoo>
    <template id="signup_fields" inherit_id="auth_signup.fields" name="Auth Signup street form field">
        <xpath expr="//div[last()]" position="after">
            <div class="form-group field-login">
                <label for="street">Street</label>
                <input type="text" name="street" id="street" class="form-control form-control-sm"/>
            </div>
        </xpath>
    </template>
</odoo>

When you click on the submit button, the web_auth_signup controller method will be called. The first thing the controller will do is call do_signup to prepare values and call the signup method of the res.users to write values to the database.

To save the street field value, you can override the _signup_with_values method:

from odoo.addons.auth_signup.controllers.main import AuthSignupHome


class AuthSignupStreet(AuthSignupHome):
    def _signup_with_values(self, token, values):
        context = self.get_auth_signup_qcontext()
        values.update({'street': context.get('street')})
        super(AuthSignupStreet, self)._signup_with_values(token, values)

Upvotes: 5

Oralover
Oralover

Reputation: 93

please check following, file names with path, in models.py file inherit res.users and add 2 fields, in main.py file added 'contact_no', 'address' and in template.xml file as per your code, added 2 fields but still i failed to have these on signup form.

custom_addons/tests/models/models.py:

class ResUsersExt(models.Model):
    _inherit = 'res.users'

    contact_no = fields.Char(string='Contact No.', required=True)
    address = fields.Char(string='Address', required=True)

addons/auth_signup/controllers/main.py:

def do_signup(self, qcontext):
    """ Shared helper that creates a res.partner out of a token """
    values = {key: qcontext.get(key) for key in ('login', 'name', 'password', 'contact_no', 'address')}
    if not values:
        raise UserError(_("The form was not properly filled in."))
    if values.get('password') != qcontext.get('confirm_password'):
        raise UserError(_("Passwords do not match; please retype them."))
    supported_lang_codes = [code for code, _ in request.env['res.lang'].get_installed()]
    lang = request.context.get('lang', '').split('_')[0]
    if lang in supported_lang_codes:
        values['lang'] = lang
    self._signup_with_values(qcontext.get('token'), values)
    request.env.cr.commit()

custom_addons/tests/views/template.xml:

<template id="signup_fields" inherit_id="auth_signup.fields" name="Auth Signup Ext form fields">
    <xpath expr="//div[last()]" position="after">
        <div class="form-group field-contact_no">
            <label for="contact_no">Contact No.</label>
            <input type="text" name="contact_no" id="contact_no" class="form-control form-control-sm"/>
        </div>
        <div class="form-group field-address">
            <label for="address">Address</label>
            <input type="text" name="address" id="address" class="form-control form-control-sm"/>
        </div>
    </xpath>
</template>

Upvotes: 2

Related Questions