Home > openerp, openobject, python > openerp – how to add a custom field to an object

openerp – how to add a custom field to an object

October 4, 2010

Adding a custom field to an existing object it’s as easy as the following few lines of code.

Let’s say you want to add a field to partner. Create a partner.py file into your module and add the following to it:

from osv import fields,osv

class res_partner(osv.osv):

    _inherit = 'res.partner'

    _columns = {
           'mytext':fields.char('My custom text field',size=64),
    }

res_partner()

Now, if you want to display this field into partner’s view you have to override the form view.

Create a partner_view.xml into your module and add:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record id="mypartner_form" model="ir.ui.view">
            <field name="name">partner.myform</field>
	        <field name="model">res.partner</field>
	        <field name="type">form</field>
	        <field name="inherit_id" ref="base.view_partner_form"/>
            <field name="arch" type="xml">
               <field name="name" position="after">
                    <field name="mytext" />
               </field>
            </field>
        </record>
    </data>
</openerp>

Then you’ll need to install your module or to upgrade it if already installed.

See openerp docs for further info.

Advertisements
Categories: openerp, openobject, python
  1. Hassanc
    October 5, 2010 at 1:54 pm

    Thanks for the tutorial. What if I want to display existing field in another view? Does this work?

    • October 5, 2010 at 3:09 pm

      if you mean an “existing field” of the object (partner in this case): yes.

  2. October 13, 2010 at 8:13 pm

    wow thanks for this useful info I’d love to try this.

  3. November 2, 2010 at 10:32 am

    Nice Tutorial, Great Work…

  4. Epo
    December 28, 2010 at 11:25 pm

    Hi ,I’m completly new with openerp. Thanks for the tutorial.

    Is that possible to define a particular modification (add fields) depending on a partner category for example ? I want to add specifics fields based on the partner category. Is that possible or should i use inheritance ?

    Thanks

    • January 9, 2012 at 11:33 am

      You can’t add field conditionally but you can display them conditionally using attrs expressions. Just grep for “attrs” into the addons folder to get all the examples you need.

  5. January 9, 2012 at 9:35 am

    This tut seems to start in the middle for newbies like me

  6. pravitha
    January 26, 2012 at 9:36 am

    hay can u please help me………..

    i want to inherit the sale.order.line form view to crm.leads form. what should i do? whatare the steps?

  7. pravitha
    January 26, 2012 at 9:37 am

    can u please add the code along with the reply

  8. January 30, 2012 at 6:04 am

    hi, can please help me.

    is there a way to group fields in tree example

    I would like to create a tree for “account_move_line” but I want to see field

    partner_id, sum(credit), sum(debit)
    group by partner

    something like that.

    -this is one step to make it drill down to details somehow

    Please help

  9. mangofunky
    March 22, 2012 at 11:18 pm

    nice post thank you very much. my question is similar to Hassanc (1st comment).

    My goal is to assign the incoterm to the supplier (res.partner) and than pull this data on the purchase order whenever I enter the supplier name with the option to alter it as occasionally the incoterm changes for the same supplier.
    I managed so far to create the field many2one in res.partner table relating to stock.incoterms table and the incoterms stores correctly on the res.partner table.
    I am stuck from this moment on trying to get the res.partner corresponding incoterm in a new field in purchase order when I enter the supplier name.
    I hope it makes sense and would be grateful for a detailed instruction on how to

    many thanks

    Mangofunky

  10. George
    May 15, 2012 at 1:44 pm

    After adding new field to existing object – OpenERP Service/daemon must be restarted before upgrading. Learned the hard way.

  11. vinodsp
    June 27, 2012 at 6:46 am

    is there any method to inherit view without affecting the base view. ie; i want to add a field to some view but i don’t want that field in all view but only inside my custom module , any chance of doing this?

  1. August 5, 2012 at 5:20 pm
Comments are closed.
%d bloggers like this: