Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
1595 Vistas

I have 2 models that inherit from res.partner, using traditional class inheritance. Each subclass adds a few custom fields.

class PartnerExtA(models.Model):
_inherit = 'res.partner'
field_a = fields.Char()

class PartnerExtB(models.Model):
_inherit = 'res.partner'
field_b = fields.Char()

I need to customise the form view of each of these subclasses, and the make the customised view the default when the model is displayed.

For example, when displaying PartnerExtA, the res.partner form should look like:

name, phone, field_a   (and no other fields)

And when displaying PartnerExtB, the form should look like:

name, phone, field_b   (and no other fields)

I've tried playing with removing and adding fields using a pattern like this:
(I've removed the leading character from the xml tags as the forum was stripping the code otherwise :/ ) 

record>
field name="name">res.partner.view.form.inherit.exta
field name="model">res.partner
field name="inherit_id" ref="base.view_partner_form"/>
field name="arch" type="xml">
xpath expr="//group" position="attributes">
attribute name="invisible">1
/xpath>

xpath expr="//group" position="after">
group invisible="not field_a">
field name="name"/>
field name="phone"/>
field name="field_a"/>
/group>
/xpath>
/field>
/record>

But things get complicated with the views interacting in strange ways. 

Is there a better way to render a different view based on the concrete subclass in Odoo 17? Can I use the QWeb templating language somehow? 

Any thoughts on this gratefully received... 

Avatar
Descartar
Mejor respuesta

In Odoo, you can customize the form view for each subclass of a model by creating separate XML views tailored to the specific fields of each subclass. Instead of manipulating the view through complex XML modifications, you create distinct form views for each subclass, specifying only the relevant fields for that subclass.

To achieve this, you define separate XML files for each subclass and configure them to display the appropriate fields. Then, you create actions that reference these views, ensuring that when a user accesses a record of a specific subclass, the corresponding form view is displayed.

Feel free to reach out if you have any further questions or need additional assistance.

Avatar
Descartar
Autor

Thanks for the reply Dishant. That's helpful. I guess this implies that I should be using Prototype inheritance rather than Class inheritance?

1. https://www.odoo.com/documentation/17.0/developer/tutorials/server_framework_101/12_inheritance.html#model-inheritance

Mejor respuesta

بنة ىىلالالخههخمنفققبي انبمر برنىثقلرمنقب ؤثنثيسنمثؤىبقثبهقخبلق ىىقثىم نرىقبنمث نبىقمنلىمق ىقثمنلقثر ثقبىقمن  

Avatar
Descartar
Autor

I'm sorry I don't speak Arabic. Google translate says this means: "the daughter of the people of the world", which doesn't seem very relevant!

Publicaciones relacionadas Respuestas Vistas Actividad
7
dic 24
7470
1
nov 24
1604
1
oct 24
1696
2
oct 24
2392
0
jul 24
1005