简体   繁体   中英

how i can show multiple field's data in a Many2one field in Odoo 14?

required these for website ( 2 requirements ) in Signup Form:

i have added a field "zone_id" in model "res.users" and Signup Form (website) which will select zone. I want to show another field value "zone_status" (a selection field) with the name of zone, like below:

Zone1 - Active

Zone2 - Active

Zone3 - InActive

etc.

how it can be done? AND also need guide to show Man2one field rows to select from list as normally in odoo's own UI it shows automatically but not in website page/form.

currently i have it as Text, how to modify it to behave/show as Many2one field:

<div class="form-group field-zone_id">
   <label for="zone_id">Your Zone</label>
   <input type="text" name="zone_id" id="zone_id" class="form-control form-control-sm"/>
</div>

For your first question you can overwrite the name_get method in the model that you have a m2o relation with and configure the concatenation string as you like. See the bellow example

def name_get(self):
    return [(record.id, record.name) for record in self]

Fleet module example link

@api.depends('name', 'brand_id')
def name_get(self):
    res = []
    for record in self:
        name = record.name
        if record.brand_id.name:
            name = record.brand_id.name + '/' + name
        res.append((record.id, name))
    return res

or make a new computed field with your string. See the below example from odoo product category link

class ProductCategory(models.Model):
    _name = "product.category"
    #...
    _rec_name = 'complete_name'
    _order = 'complete_name'

    name = fields.Char('Name', index=True, required=True)
    complete_name = fields.Char(
        'Complete Name', compute='_compute_complete_name',
        store=True)

    #...

    @api.depends('name', 'parent_id.complete_name')
    def _compute_complete_name(self):
        for category in self:
            if category.parent_id:
                category.complete_name = '%s / %s' % (category.parent_id.complete_name, category.name)
            else:
                category.complete_name = category.

For the second question you can read the data like so. See the below example form website_sale module link

<div t-attf-class="form-group #{error.get('country_id') and 'o_has_error' or ''} col-lg-6 div_country">
    <label class="col-form-label" for="country_id">Country</label>
    <select id="country_id" name="country_id" t-attf-class="form-control #{error.get('country_id') and 'is-invalid' or ''}">
        <option value="">Country...</option>
        <t t-foreach="countries" t-as="c">
            <option t-att-value="c.id" t-att-selected="c.id == (country and country.id or -1)">
                <t t-esc="c.name" />
            </option>
        </t>
    </select>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM