我刚刚意识到用户无法像Many2many
字段那样通过界面订购One2many
字段。
在标准One2many
字段中,您会看到协模型的树视图及其字段。如果单击One2many
的字段名(列名),则其树视图将由您单击的字段自动排序(如果此字段存储在数据库中),则会看到一个小箭头图标。
但是在标准Many2many
字段中,如果单击任何列的名称(该字段存储在数据库中无关紧要),它什么都不做,就无法排序其树形视图。
实际上,如果在default_order
中打开树,则XML树属性Many2many
似乎不起作用。
如果我将其添加到协模型的Python定义中,唯一仍在工作的是_order
。
例如:
我有一个Many2many
字段,指向account.treasury.forecast.invoice
。我不告诉该字段打开任何特定的视图,因此默认情况下会加载以下树形视图:
<record model="ir.ui.view" id="account_treasury_forecast_invoice_tree_view">
<field name="name">account.treasury.forecast.invoice.tree</field>
<field name="model">account.treasury.forecast.invoice</field>
<field name="arch" type="xml">
<tree string="Invoices" widget="many2many">
<field name="date_due" />
<field name="invoice_id" />
<field name="partner_id"/>
<field name="journal_id" />
<field name="base_amount" sum="Total Base" />
<field name="tax_amount" sum="Total Taxes" />
<field name="total_amount" sum="Total" />
<field name="residual_amount" sum="Total Residual" />
<field name="state" />
</tree>
</field>
</record>
我已经意识到,如果在default_order
字段中打开以下视图,Many2many
不会执行任何操作。
<record model="ir.ui.view" id="account_treasury_forecast_invoice_tree_view">
<field name="name">account.treasury.forecast.invoice.tree</field>
<field name="model">account.treasury.forecast.invoice</field>
<field name="arch" type="xml">
<tree string="Invoices" widget="many2many" default_order="date_due">
<field name="date_due" />
<field name="invoice_id" />
<field name="partner_id"/>
<field name="journal_id" />
<field name="base_amount" sum="Total Base" />
<field name="tax_amount" sum="Total Taxes" />
<field name="total_amount" sum="Total" />
<field name="residual_amount" sum="Total Residual" />
<field name="state" />
</tree>
</field>
</record>
至少_order
有用:
class AccountTreasuryForecastInvoice(models.Model):
_name = 'account.treasury.forecast.invoice'
_description = 'Treasury Forecast Invoice'
_order = 'date_due'
但是问题是该界面不允许用户按其任何字段进行排序,而我想允许他们有时按date_due
和有时按invoice_id
进行订购。我该如何处理?您在Many2many
字段中遇到同样的问题吗?