Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
319 Lượt xem

Bonjour à tous,

Je travaille avec Odoo 17 et j'essaie de créer un filtre qui regroupe les enregistrements par write_date et stage_id . Cependant, je rencontre des problèmes de syntaxe dans la balise <filter> .

Lorsque j'essaie d'utiliser une liste dans l'attribut de contexte comme ceci :

xml

Modificateur de copieur

< filter string = "État de la semaine" name = "group_week_state" context = "{'group_by': ['write_date', 'stage_id']}" />

Je reçois une erreur indiquant que l'attribut de contexte doit être une chaîne et non une liste Python.

Si j'essaie de le transmettre sous forme de chaîne séparée par des virgules à la place :

xml

Modificateur de copieur

< filter string = "État de la semaine" name = "group_week_state" context = "{'group_by': 'write_date,stage_id'}" />

Il applique uniquement le regroupement par write_date et ignore stage_id .

Quelqu'un a-t-il réussi à appliquer plusieurs champs group_by dans un seul filtre dans Odoo 17 ? J'apprécierais toute aide ou solution de contournement.

Merci d'avance!


Ảnh đại diện
Huỷ bỏ
Tác giả

Thank you very much, christoph your response was exactly what i need thank you very much, and big thank you to cybrosys

Câu trả lời hay nhất

Only known way to combine multiple fields in a predefined group_by is by defining a record for the ir.filters model.

This is also the only case in which Odoo combines two or more fields in a group_by in the UI by default that can be select by a user*. See for example https://github.com/odoo/odoo/blob/17.0/addons/account/report/account_invoice_report_view.xml#L61

To apply this approach, for example on the res.partner model, you would do the following:

view/res_partner_views.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
    <record id="filter_country_salesperson_group" model="ir.filters">
        <field name="name">By Country and Salesperson</field>
        <field name="model_id">res.partner</field>
        <field name="domain">[]</field>
        <field name="user_id" eval="False"/>
        <field name="context">{'group_by': ['country_id', 'user_id']}</field>
    </record>
</odoo>

Which would result in a filter stored as a favorite:


... and is essentially the same what Odoo does for example in the Invoicing App (technical name account):


A direct integration into the Group By-section does not seem to be possible.

If you want to prevent users from deleting (or changing) this particular filter (which could be easily done, since its a filter stored as a favorite just like any other) you could do a super().write() and super().unlink() extension checking for this particular record and raise an UserError when an attempt on changing or deleting is detected.



*) You can pass multiple pre-defined group_by filters when using a dedicated action, but according to your question and example source, this is not what you are looking for.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,


In Odoo, the <filter> tag's context attribute must be a valid Python dictionary string, and the 'group_by' key can only accept a single field name (string), not a list of fields, even though Odoo's ORM supports grouping by multiple fields in backend code.


Try with any of the following.


1: Use the web UI (manual grouping)

          Just advise your users to manually group by both fields using the “Group By” > “Add Custom Group” option in the search view. This is the safest option if you want to avoid custom modules.


 2: Predefine a custom action (server-side context)

          You can create a custom ir.actions.act_window that pre-sets multiple group_by fields using Python:

              {

    'name': 'Group by Write Date and Stage',
'type': 'ir.actions.act_window',
'res_model': 'your.model',
'view_mode': 'tree,form',
'context': {
'group_by': ['write_date', 'stage_id'],
},
}


This works in server-side Python because the 'group_by' can be a list in that context.

Then link to this action from a menu item.


Hope it helps

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 6 25
424
1
thg 1 25
1228
0
thg 8 24
224
0
thg 7 25
765
1
thg 5 25
1508