Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
14766 มุมมอง

How can i make a new field be auto increment? I added a new field for the res.partner and i want it to be automatically filed with a auto infringement number if the partner i category is not number 1. Any easy way to make this? Could it be done using the sequences?

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

In your .xml file :

<?xml version="1.0"?>

<openerp>
<data>
<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>
</data>
    <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">6</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">6</field>
        </record>
</data>
</openerp>

In your .py file :

# -*- coding: utf-8 -*-

from openerp.osv import fields, osv

import openerp.addons.decimal_precision as dp

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()
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

You can make a record for sequence. here is example to make sequence. Create new xml file.

<openerp> <data noupdate="1">

    <!-- Sequences for sale.order -->
    <record id="seq_unique_id" model="ir.sequence.type">
        <field name="name">my_sequence</field>
        <field name="code">sequence_code</field>
    </record>

    <record id="seq_unique_id2" model="ir.sequence">
        <field name="name">my_sequence</field>
        <field name="code">sequence_code</field>
        <field name="prefix">prefix</field>
        <field name="padding">3</field>
    </record>

</data>

</openerp>

after creating the file you need to initialise it in the __openerp__,py file.

Then in your model.py file where your field is defined in _columns. you need to set default value for that using.

_columns = {
    'new_field': fields.char('New Field', size=16),
}
_defaults = {
        'new_field': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'sequence_code'),
}

update the module, Now whenever you will create a record it will get a sequence from the database.

It will help you.

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

I looked all over and the only tutorial or explanation that was in any way helpful is this excellent YouTube video from Odoo Mates, which completely solved the problem of adding auto-incrementing behaviour to a field

https://www.youtube.com/watch?v=Cz5eM5FDmTE

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
2
ธ.ค. 22
7471
3
ต.ค. 20
9915
3
ก.ย. 17
8566
0
มี.ค. 15
4885
0
ก.พ. 25
1424