【问题标题】:How can I show multiple fields' data in a Many2one field in Odoo 14?如何在 Odoo 14 的 Many2one 字段中显示多个字段的数据?
【发布时间】:2021-05-18 03:51:58
【问题描述】:

这些是注册表中网站所必需的(2 个要求):

我在模型res.users 和注册表单(网站)中添加了一个字段zone_id,它将选择一个区域。我想用区域名称显示另一个字段值zone_status(选择字段),如下所示:

Zone1 - Active

Zone2 - Active

Zone3 - InActive

等等

怎么做?而且我还需要在 Odoo 自己的 UI 中显示 Many2one 字段行以从列表中选择的指南。它会自动显示,但不在网站页面/表单中。目前,我将它作为文本,如何修改它以表现/显示为 Many2one 字段:

<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>

【问题讨论】:

    标签: python web frameworks odoo


    【解决方案1】:

    对于您的第一个问题,您可以覆盖与您有 m2o 关系的模型中的 name_get 方法,并根据需要配置连接字符串。请看下面的例子

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

    队列模块示例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
    

    或者用你的字符串创建一个新的计算域。请参阅下面来自 odoo 产品类别的示例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.
    

    对于第二个问题,您可以像这样读取数据。请参阅下面的示例表单 website_sale 模块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>
    

    【讨论】:

    • 感谢 @Amr Ad-Alkrim 提供详细指南,我已在此处部署您的最后一个代码,以填充网站表单上的 many2one 字段以进行选择。应用升级没有错误,但运行注册表单时显示错误:渲染编译 AST AttributeError 时出错:'NoneType' 对象没有属性'get' 模板:auth_signup.fields 路径:/t/div[6]
    • 我没有得到使用的东西(例如:使用的模型,t-foreach=countries",c.id == (country and country.id or -1) etc.)是什么在这里因为我的知识很少,对于这里的两个问题,我的模型/python 文件:class ResUsersExt(models.Model): _inherit = 'res.users' contact_no = fields.Char(string='Contact No.') address = fields.Text(string='你的地址', required=True, default='00') zone_id = fields.Many2one('tests.zones', string="Your Zone", required=True, default="1")
    猜你喜欢
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多