0

在模型中,我添加了一个teacher_id与 相关的 Many2one 字段res.users,然后添加teacher_gender了与相关的字段teacher_id.gender

teacher_id = fields.Many2one('res.users', string='Teachers', required=True, tracking=True)
teacher_gender = fields.Selection([('male', 'Male'), ('female', 'Female'), ('other', 'Other')], string='Gender', related='teacher_id.gender', tracking=True)

在视图中,我添加了这样的内容:

<record id="school_teachers_search" model="ir.ui.view">
    <field name="name">school.teachers.search</field>
    <field name="model">school.teachers</field>
    <field name="arch" type="xml">
        <search string="Teachers">
            <field name="name"/>
            <field name="photo"/>
            <field name="teacher_age"/>
            <field name="teacher_dob"/>
            <field name="teacher_gender"/>
            <field name="teacher_blood_group"/>
            <field name="nationality"/>
            <separator/>
            <filter name="teacher_age_old" string="Age More Than 40" domain="[('teacher_age', '>=', 40)]"/>
            <filter name="teacher_age_young" string="Age Less Than 40" domain="[('teacher_age', '&lt;', 40)]"/>
            <filter name="teacher_gender" string="Male" domain="[('teacher_gender', '=', 'male')]"/>
            <filter name="teacher_gender" string="Female" domain="[('teacher_gender', '=', 'female')]"/>
            <separator/>
            <!-- filter for archive / unarchive -->
            <filter string="Archived" name="inactive" domain="[('active', '=', False)]"/>
            <separator/>
            <group expand="0" string="Group By">
                <!-- group by name is difference than field name, or else will be issue when set context in ir.actions.act_window below -->
                <filter string="Gender" name="group_teacher_gender" domain="[]" context="{'group_by': 'teacher_gender'}"/>
            </group>
            <!-- search panel is a left sidebar filter -->
            <searchpanel>
                <!-- add searchpanel teacher_gender to this view causing issue -->
                <field name="teacher_gender" string="Gender" icon="fa-cutlery" color="#875A7B" enable_counters="1"/>
                <field name="teacher_blood_group" select="multi" string="Blood Group" icon="fa-truck" enable_counters="1"/>
            </searchpanel>
        </search>
    </field>
</record>

<record model="ir.actions.act_window" id="action_school_teachers">
    <field name="name">teachers</field>
    <field name="res_model">school.teachers</field>
    <field name="view_mode">tree,form,kanban</field>
    <field name="domain">[]</field>
    <!-- set default group by and filter -->
    <!-- add search_default_group_teacher_gender to this view causing issue -->
    <field name="context">{"search_default_group_teacher_gender": 1, "search_default_teacher_age_old": 1}</field>
    <field name="help" type="html">
        <p class="oe_view_nocontent_create">Create new teacher
        </p>
    </field>
</record>

之后,当我来到教师页面时,我遇到了这个问题:

字段 school.teachers.teacher_gender 不是存储字段,只有存储字段(常规或 many2many)对 'groupby' 参数有效

在此处输入图像描述

您可以在这篇文章中搜索“导致问题”,并查看导致问题的代码,我已评论这些代码以忽略该问题,但我仍然想知道如何解决问题。

您可以提交此模块的完整代码:
https ://github.com/saxsax1995/odoo-15-school/blob/master/school/models/teachers.py
https://github.com/saxsax1995/odoo-15 -school/blob/master/school/views/teachers.xml

请帮忙,谢谢。

4

0 回答 0