Reputation: 168
I have an issue where columns are not sortable on certain related fields.
I have extended the res.partner form with the following code snippet:
class CrmProject(models.Model):
_name = 'crm.project'
_inherit = ['mail.thread', 'ir.needaction_mixin']
_inherits = {
'res.partner': 'partner_id',
}
_order = "name asc"
@api.model
def _get_domain_employee(self):
ids = self.env.ref('aim_contacts.res_partner_business_2').ids
return [('business_id', '=', ids)]
.
.
industry_id = fields.Many2one('crm.project.industry', 'Construction Type')
I have extended the sales/opportunity module with the following snippet:
class CrmOpportunity(models.Model):
_name = 'crm.opportunity'
_inherit = ['mail.thread', 'ir.needaction_mixin', 'aim.res.common']
_order = 'name asc'
@api.model
def _get_domain_employee(self):
ids = self.env.ref('aim_contacts.res_partner_business_2').ids
return [('business_id', '=', ids)]
name = fields.Char('Topic', required=True)
.
.
crm_project_id = fields.Many2one('crm.project', 'Project', track_visibility='onchange')
industry_id = fields.Many2one('crm.project.industry', 'Construction Type',related='crm_project_id.industry_id', readonly=True)
The view is defined as follows:
<record id="crm_opportunity_tree_view" model="ir.ui.view">
<field name="name">crm.opportunity.tree.view</field>
<field name="model">crm.opportunity</field>
<field name="arch" type="xml">
<tree string="Opportunities">
<field name="name" string="Topic" invisible="1"/>
<field name="crm_project_id"/>
<field name="industry_id"/>
</tree>
</field>
</record>
Issue 1: When the opportunities page is loaded, clicking on the "Construction Type" (industry_id) header does nothing.
Issue 2: Also, when clicking on the "Project" (crm_project_id) header, the column is sorted by id rather than name (even though it is displaying name).
I thought issue 2 could be resolved by adding to crm.project.py:
_order = "name asc"
but it did not work.
Any help would be greatly appreciated.
Update
The method of adding store=True to related fields works. There is an issue though that if the field is already created, then it will/may not update with the most resent data.
To get around this, I created another field with the same information as the original, and then linked to it. I have read that others drop the fields and create them again.
Upvotes: 2
Views: 2549
Reputation: 10189
Related fields are not stored in the database, and you can't order by a field which is not stored in the database, so the solution to that part is to add a store=True
to the related field:
industry_id = fields.Many2one('crm.project.industry', 'Construction Type',related='crm_project_id.industry_id', readonly=True, store=True)
Your Project field should be working OK, have you checked in the tree view of crm.project
if the records are ordered by name? Check also the name_get
method of crm.project
and its display_name
field. You can solve this problem adding other stored related field to your crm.opportunity
model:
crm_project_name = fields.Char('crm.project', 'Construction Type',related='crm_project_id.name', store=True)
And in your XML:
<record id="crm_opportunity_tree_view" model="ir.ui.view">
<field name="name">crm.opportunity.tree.view</field>
<field name="model">crm.opportunity</field>
<field name="arch" type="xml">
<tree string="Opportunities">
<field name="name" string="Topic" invisible="1"/>
<field name="crm_project_id" invisible="1"/>
<field name="crm_project_name"/>
<field name="industry_id"/>
</tree>
</field>
</record>
Do this if you're in a hurry (while you're looking for the problem). But I'll do the related field to name
and also the related field to display_name
, and order them in the tree view to check what happens (if display_name
column is ordered by ID, then the problem is the name_get
method or the display_name
field, no doubt).
Upvotes: 4