'Field is not a stored field, only stored fields (regular or many2many) are valid for the 'groupby' parameter in Odoo 15
In the model, I added a Many2one field that is teacher_id
and related to res.users
, then i added teacher_gender
that related to 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)
And in the view, I added like this:
<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', '<', 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>
And after that, i get this issue when i come to teacher page:
Field school.teachers.teacher_gender is not a stored field, only stored fields (regular or many2many) are valid for the 'groupby' parameter
You can search "causing issue" in this post, and see that which code causing the issue, i've comment those codes to ignore the issue, but i still want to know how to fix the problem.
Full code of this module you can file in:
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
Please help, thanks.
Solution 1:[1]
Add store = True
when defining teacher_gender
as a related field. Under the hood, related field is nothing but a kind of calculated field. If you want to use any calculated field in a search or group or filter in odoo then you need to store it in the database. By default related fields are not stored.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jeremy Caney |