Skip to Content
Menu
This question has been flagged
4 Replies
7642 Views

Hi Team,

I am new to Odoo, could you please help me understand which is the standard approach to add new fields to an odoo delivered model.

Say, if I need to create two new additional fields (profession and hobby) to existing CRM_LEAD model.

I understand that the basic approach is to add those new fields to the _inherit object of CRM_LEAD model

class AddNewfield(models.Model):

_inherit = "crm.lead"

Profession = fields.Char('Profession', size=20, required=True)

Hobby = fields.Char('Hobby', size=20)

But I had come across article stating that its not advisable to add new fields to odoo delivered model, as this might impact future upgrade.

Then, should I have to build a new model adding these two fields and a many2one field to the parent table.

class AddNewModel(models.Model):

_name = "crm.AddNewModel"

Profession = fields.Char('Profession', size=20, required=True)

Hobby = fields.Char('Hobby', size=20)

lead_id = fields.Many2one('crm.lead', required=True)

Let me know if which is the right approach.

As well how will I be able to display the new fields from model crm_AddNewModel in the Leads standard form view.

Avatar
Discard

1st approach is right one, but create a new module for do those changes don't do in "CRM" or any standard module.

Best Answer

Hi, if you use the _name attribute in your class you are inheriting the model creating a new (different) one. If you omit the _name, you will inherit from the model extending it. In the first case you will inherit the view definition, but not its design that must be recreated from scratch (or so its seems; I had o do that and have a question open on this matter), in the second case you will operate directly in the standard model/view.

To add a field follow the link given by Temur and read the docs. In the case of extension, i.e. without _name, you will simply need to create your extended view (an XML file, with the correct reference) and add the fields in the desired position with <field name="filedX" position ="before/after..."> or using an Xpath expression (if the name is not unique). You can find a good, simple and readable example in the Geo Localization module (that adds some fields to res.partner). To know the name of the fields you can activate the developer mode and the name of the fields will appear on the screen when you pass the mouse over a field.

This code will add my custom field (titolo) in res.partner view after the field "website" adding a label to the new field and a placeholder (a suggestion) for the field:

<!-- Custom Field -->

<label for="titolo" string="Titolo"/>

<xpath expr="//field[@name='website']" position="after">

<field name="titolo" placeholder="SPETTABILE STUDIO DENTISTICO" />

</xpath>

Avatar
Discard
Best Answer

By stating:

_inherit = "crm.lead" 

You are in fact inheriting an Odoo model/class/object, and adding your fields to it.  This is the correct way.  You ARE creating your own model with this method. 

The CRM module from Odoo actually inherits and extends res_partner with new fields (although the the old API): 

https://github.com/odoo/odoo/blob/9.0/addons/crm/res_partner.py 

I agree with Temur, check out the documentation for a more detailed explanation.

Avatar
Discard
Best Answer

I think you can start with the Odoo documentation "Building a Module" at https://www.odoo.com/documentation/8.0/howtos/backend.html

Avatar
Discard
Best Answer

I think a relation is too much hassle, you better check the Delegation Inheritance to "embed" your model into "crm.lead" but keeping your data on another table transparently.

About the Views, you may enable the debug mode (/web?debug=1) and manage the current view (Bug icon at the top-right) to see what's the one you need to inherit from and add your fields there.

Avatar
Discard
Related Posts Replies Views Activity
1
Aug 23
1927
0
Apr 23
1702
1
Dec 21
3410
5
Jun 21
15756
1
Mar 21
4691