Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

How to add a field auto-incremented to the customer form? [Closed]

By
Luís
on 3/27/13, 7:46 AM 7,317 views

The Question has been closed

by
Luís
on 04/23/2013 14:54:40

Hi, I need to add a field that every time that a customer is added , a sequential number is given to the new customer. Basically what I want is to create a field that it would be an incremental number. Do you Know how to do this? Thanks

Like Francesco Apruzzese and duh386 suggest I use this code for my py file and xml file:

py file

class res_partner(osv.osv):
    _inherit = 'res.partner'
    _name = 'res.partner'

    def _n_client(self, cr, uid, vals, context=None):
        if vals.get('n_client','/')=='/':
            vals['n_client'] = self.pool.get('ir.sequence').get(cr, uid, 'res.partner') or '/'
        return super(res_partner, self).create(cr, uid, vals, context=context)

    _columns = {
        'n_client' : fields.function(_n_client, string='Client Number', type='int'),
    }
    _defaults = {
        'n_client' : _n_client,
    }
res_partner()

xml file

<record id="view_mutante_form" model="ir.ui.view">
        <field name="name">res.partner.form.inherit</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form" />
        <field name="arch" type="xml">
        <xpath expr="/form/sheet/group/group/label[@for='street']" position="before">
            <field name="n_client"/> 
        </xpath>
    </field>
</record>

But with this code I get this error:

Uncaught Error: Widget type 'undefined' is not implemented

http://localhost:8069/web/webclient/js:3447


I make a few changes for py file:

class res_partner(osv.osv):
    _inherit = 'res.partner'
    _name = 'res.partner'

    _columns = {
        'n_client' : fields.char('Client Number', size=64, readonly=True),
    }
    def create(self, cr, uid, vals, context=None):
        if vals.get('n_client','/')=='/':
            vals['n_client'] = self.pool.get('ir.sequence').get(cr, uid, 'res.partner') or '/'
        return super(res_partner,self).create(cr, uid, vals, context=context)
res_partner()

But now when I create a new customer in my new field (field that I create - Client Number), in this field appear "/" every time that I create a new customer. So ir.sequence isn't working.

Do you know what I did wrong?

Thanks

The code is wrong. Please, read my code because there are some errors. n_client field will be a char and _n_client function must be "create" function and _defaults dictionary mst be deleted

Francesco OpenCode
on 3/27/13, 2:26 PM

When you write super(res_partner, self).create(cr, uid, vals, context=context), you are telling to openerp to call the original create function after your code snippet. Go sure! Go to the dark side of OpenERP :D

Francesco OpenCode
on 3/27/13, 2:50 PM

I though that if I do that (the name of the function = "create") the code from the function "create" in res_partner.py would be overwrite, because of this " _inherit = 'res.partner'", but I'm a newbie...

Houda
on 6/12/14, 10:08 AM
2
Luís
On 4/23/13, 2:51 PM

On mymodule.py I inherit res.partner and I add a field for customer and supplier:

class res_partner(osv.osv):
_inherit = 'res.partner'
_name = 'res.partner'

_columns = {
    'n_client' : fields.char('Client Number', size=64, readonly=True),
    'n_supplier' : fields.char('Supplier Number', size=64, readonly=True),
}
_sql_constraints = [
    ('name_uniq_1', 'unique(n_client)', 'Number of client must be unique!'),
    ('n_supplier_uniq', 'unique(n_supplier)', 'Number of supplier must be unique!'),        
]

def create(self, cr, uid, vals, context=None):   

    if vals.get('n_client') == None:
        vals['n_client'] = self.pool.get('ir.sequence').get(cr, uid, 'res.partner.customer')

    if vals.get('n_supplier') == None:
        vals['n_supplier'] = self.pool.get('ir.sequence').get(cr, uid, 'res.partner.supplier')
    return super(res_partner,self).create(cr, uid, vals, context)

def copy(self, cr, uid, id, default=None, context=None):
    default.update({
        'n_client': self.pool.get('ir.sequence').get(cr, uid, 'res.partner.customer'),
        'n_supplier': self.pool.get('ir.sequence').get(cr, uid, 'res.partner.supplier')
    })
    return super(res_partner, self).copy(cr, uid, id, default, context)
res_partner()

On my mymodule_view.xml:

<record id="view_mutante_form" model="ir.ui.view">
        <field name="name">res.partner.form.inherit</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form" />
        <field name="arch" type="xml">
    <xpath expr="/form/sheet/group/group/label[@for='street']" position="before">
        <field name="n_client" attrs="{'invisible':[('customer','!=',True)]}"/>
        <field name="n_supplier" attrs="{'invisible':[('supplier','!=',True)]}"/>
    </xpath>
    </field>
</record>

And the next step is create a new file [yourmodule]_sequence.xml.

mymodule_sequence.xml:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="1">
        <record model="ir.sequence.type" id="seq_type_res_partner">
            <field name="name">number_client_sequence</field>
            <field name="code">res.partner.customer</field>
        </record>
        <record model="ir.sequence" id="seq_res_partner">
            <field name="name">number_client_sequence</field>
            <field name="code">res.partner.customer</field>
            <field name="prefix">C</field>
            <field name="padding">5</field>
        </record>
        <record model="ir.sequence.type" id="seq_type_res_supplier">
            <field name="name">number_supplier_sequence</field>
            <field name="code">res.partner.supplier</field>
        </record>
        <record model="ir.sequence" id="seq_res_supplier">
            <field name="name">number_supplier_sequence</field>
            <field name="code">res.partner.supplier</field>
            <field name="prefix">S</field>
            <field name="padding">5</field>
        </record>
</data>
</openerp>
2

Francesco OpenCode

--Francesco OpenCode--
3608
| 5 7 9
Grottaglie, Italy
--Francesco OpenCode--

Italian Odoo (OpenERP) Modules Developer LINKEDIN: http://www.linkedin.com/in/francescoapruzzese

Francesco OpenCode
On 3/27/13, 8:12 AM

You can see how sale.order do to create the sale number with ir.sequence:

def create(self, cr, uid, vals, context=None):
        if vals.get('name','/')=='/':
            vals['name'] = self.pool.get('ir.sequence').get(cr, uid, 'sale.order') or '/'
        return super(sale_order, self).create(cr, uid, vals, context=context)

Where 'name' will be your field name

Can you explain what is "vals" ? And what is the meaning of '/'?

Versão Integral, Anabela Damas
on 3/27/13, 12:31 PM

vals is the dictionary that contain the parameters that will saved in database. '/' is the default value if 'name' is not passed.

Francesco OpenCode
on 3/27/13, 1:00 PM
2
duh386
On 3/27/13, 8:04 AM

You can write your function for calculating default value, something like

def _your_function(self, cr, uid, context=None):
    ...Some actions or sql-queries...
    return value

_defaults = {
    'field_name': _your_function,
}

Or you can use standard sequence functional (with using model ir.sequence).

In OpenERP you better have to use the ir.sequence object that way you can even use specific format of your incremented value. Furthermore, setting it in _default is dangerous as default will be called even if you don't discard a created object. And to be mentionned that default might be called on opening the form plus on creating the object. That's why I down vote your answer.

Camptocamp SA
on 3/28/13, 2:25 PM

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 3/27/13, 7:46 AM
Seen: 7317 times
Last updated: 3/16/15, 8:10 AM