如何在OpenERP 7中添加自动增量字段?

如何在OpenERP 7中添加自动增量字段?,openerp,auto-increment,openerp-7,Openerp,Auto Increment,Openerp 7,我搜索并修改了openerp的一个简单自定义模块的源代码,下面给出了代码 init.py import sim { 'name': 'Student Information Management', 'version': '0.1', 'category': 'Tools', 'description': """This module is for the Student Information Management.""", 'author': 'Mr Praveen Srinivasan'

我搜索并修改了openerp的一个简单自定义模块的源代码,下面给出了代码

init.py

import sim
{
'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,
}
openerp.py

import sim
{
'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>

一切正常,但我想添加一个自动递增的注册Id字段。我在网上搜索了怎么做,但找不到合适的解决办法。请帮助我。

按ir.sequence创建记录。首先将您的注册号字段设置为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>

注册类型
注册码
规则
注册码
规则
3.
在py文件中,可以定义何时生成序列。在默认情况下获取默认注册号,或覆盖create方法并调用sequence,或在任何其他方法中:


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

创建序列文件后,您可以将此函数添加到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)

此功能将正常工作

我不太理解。是否希望在创建记录时增加字段“注册号”?像销售订单名称是递增的吗?我可以确认它工作得最好。另一个答案总是跳过一个数字,即它看起来像
0001
0003
,等等。