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

Hi,

I'm trying to override the default form view that's opened from the Contacts menu (i.e., contacts.action_contacts) with my custom form view.

To achieve this, I've added the following record to my custom module:

<record id="action_contacts_view1" model="ir.actions.act_window.view">
    <field eval="2" name="sequence"/>
    <field name="view_mode">form</field>
    <field name="view_id" ref="my_module.view_custom_approval_form"/>
    <field name="act_window_id" ref="contacts.action_contacts"/>
</record>

This causes error:

2025-05-06 02:01:54,959 11372 ERROR odoo2 odoo.sql_db: bad query: b'INSERT INTO "ir_act_window_view" ("act_window_id", "create_date", "create_uid", "sequence", "view_id", "view_mode", "write_date", "write_uid") VALUES (153, \'2025-05-06 02:01:51.694748\', 1, 2, 1320, \'form\', \'2025-05-06 02:01:51.694748\', 1) RETURNING "id"'

ERROR: duplicate key value violates unique constraint "act_window_view_unique_mode_per_action"

DETAIL:  Key (act_window_id, view_mode)=(153, form) already exists.

he only workaround I've found is to manually delete the existing view from the database before installing:

DELETE FROM ir_act_window_view
WHERE act_window_id = (
    SELECT res_id
    FROM ir_model_data
    WHERE module = 'contacts'
      AND name = 'action_contacts'
      AND model = 'ir.actions.act_window'
)
AND view_mode = 'form';

I understand this is not a clean or safe solution.

What is the correct and clean way to override the default form view for contacts.action_contacts so that my custom form is always used without manual SQL steps?

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

This should work:


<record id="contacts.action_contacts_view_form" model="ir.actions.act_window.view">
    <field name="view_id" ref="my_module.view_custom_approval_form"/>
</record>


With this you're essentially overriding the definition of <module>.<record>, in this case, Module:contacts (View-)Record:action_contacts_view_form

In fact, this could still be problematic upon uninstalling your module, however, it's probably your best bet.

You can find several references to this approach throughout the core source, i.e. by doing a regex search for 

<record.*\.action_


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

Thanks you so much!

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 6 25
542
1
thg 5 25
704
1
thg 4 25
1125
1
thg 5 25
716
2
thg 4 25
1072