How to add autoincremental field in OpenERP 7?

假如想象 提交于 2019-12-06 03:29:32

问题


I searched and modified the source code of a simple custom module of openerp, I give the code below

init.py

import sim

openerp.py

{
'name': 'Student Information Management',
'version': '0.1',
'category': 'Tools',
'description': """This module is for the Student Information Management.""",
'author': 'Mr Praveen Srinivasan',
    'website': 'http://praveenlearner.wordpress.com/',
'depends': ['base'],
'data': ['sim_view.xml'],
'demo': [],
'installable': True,
    'auto_install': False,
    'application': True,
}

sim_view.xml

<?xml version="1.0"?>
<openerp>
<data>
<!-- ============== student================= -->
<!-- 1st part of the sim_view start-->
<record model="ir.ui.view" id="student_form">
<field name="name">Student</field>
<field name="model">sim.student</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Student" version="7.0">
<group>
<field name="reg_no"/>
<field name="student_name"/>
<field name="father_name"/>
<field name="gender"/>
<field name="contact_no"/>
<field name="address"/>
</group>
</form>
</field>
</record>
<!-- 1st part of the sim_view end-->

<!--2nd part of the sim_view start-->
<record model="ir.ui.view" id="student_tree">
<field name="name">Student</field>
<field name="model">sim.student</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="Student">
<field name="reg_no"/>
<field name="student_name"/>
<field name="father_name"/>
<field name="gender"/>
<field name="contact_no"/>
<field name="address"/>
</tree>
</field>
</record>
<!--2nd part of the sim_view end-->

<!-- 3rd part of the sim_view start-->
<record model="ir.actions.act_window" id="action_student">
<field name="name">Student</field>
<field name="res_model">sim.student</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<!--3rd part of the sim_view end-->

<!--4th part of the sim_view start-->
<menuitem name="SIM/Student/StudentInfo" id="menu_sim_student"  
      action="action_student"/>
<!--4th part of the sim_view end-->
</data>
</openerp>

sim.py

**

    from openerp.osv import fields, osv
    class student(osv.osv):
        _name = "sim.student"
        _description = "This table is for keeping personal data of student"
        _columns = {
            'reg_no': fields.integer('Registration Number',size=7,required=True),
            'student_name': fields.char('Student Name',size=25,required=True),
            'father_name': fields.char("Father's Name",size=25),
            'gender':fields.selection([('male','Male'),('female','Female')],'Gender'),
            'contact_no':fields.char('Contact Number',size=10),
            'address':fields.char('Address',size=256)
        }
        _sql_constraints = [
        ('uniq_name', 'unique(reg_no)', 'This Reg.No is number already registered!') 
        ]

    student()

**

All is working good, but I want to add an auto incremented registration Id field. I searched the Internet how to do it, but I can't get a proper solution. Please help me.


回答1:


After creating sequence file you can add this function to your sim.py

def create(self, cr, uid, vals, context=None):
    sequence=self.pool.get('ir.sequence').get(cr, uid, 'reg_code')
    vals['reg_no']=sequence
    return super(student, self).create(cr, uid, vals, context=context)

This function will work properly




回答2:


Create a record in ir.sequence. First make your reg_no field to char.

<record id="seq_type_1" model="ir.sequence.type"> 
  <field name="name">REG Type</field> 
  <field name="code">reg_code</field> 
</record> 
<record id="seq_1" model="ir.sequence"> 
    <field name="name">reg</field> 
    <field name="code">reg_code</field> 
    <field name="prefix">REG</field> 
    <field name="padding">3</field> 
</record>

In your py file you can define when to generate the sequence. Either in defaults to get default reg number or override the create method and call the sequence Or in any other methods:

_defaults = { 'reg_no': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'reg_code'), }



来源:https://stackoverflow.com/questions/22477712/how-to-add-autoincremental-field-in-openerp-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!