如何实现sale.order.line中的sequence字段自增?
-
OpenERP 7.0
sale.order.line中的sequence字段默认值为10,通过widget="handle"显示在界面上,而不是直接的数字<br /> <tree string="Sales Order Lines" editable="bottom"><br /> <field name="sequence" widget="handle"/><br />
现在想要将sequence再次显示在订单行中,
视图部分简单:<br /> <xpath expr="//tree/field[@name='product_id']" position="before"><br /> <field name="sequence" string="序号"/><br /> </xpath><br />
但希望每个订单中,sequence字段从1开始自动增号,并且添加一个订单明细时,如果取消当前添加项目后,再添加新项目时不跳号,如何实现呢?
试了shelly神的改代码打法, [检测到链接无效,已移除]
'sequence': lambda self, cr, uid, context=None: self.pool.get('ir.sequence').get(cr, uid, 'sale.order.line'),
失败了....
期待大神们现身说法... -
在创建时给予赋值,意味着你的ir.sequence在你点击添加项目的时候就已经生存了一个sequence,你取消订单行,并不能改变ir.sequence表中的值
你可以在保存的时候在给sequence赋值,做法就是参照saler.order中的name的写法,改写create的方法(点击保存的时候才调用create,也就只是你想保存的时候才生存sequence)<br /> _defaults = {<br /> 'sequence': '/',<br /> }<br /><br /> def create(self, cr, uid, vals, context=None):<br /> if vals.get('sequence','/')=='/':<br /> vals['sequence'] = self.pool.get('ir.sequence').get(cr, uid, 'sale.order.line') or '/'<br /> return super(sale_order_line, self).create(cr, uid, vals, context=context)<br />
-
ValueError: invalid literal for int() with base 10: '/'<br />
鉴于sequence是整数,默认值改为: 1
sale.py# -*- coding: utf-8 -*-<br /><br />from openerp.osv import osv, fields<br /><br />class sale_order_line(osv.osv):<br /><br /> _inherit = "sale.order.line"<br /><br /> _defaults = {<br /> 'sequence': '1',<br /> }<br /><br /> def create(self, cr, uid, vals, context=None):<br /> if vals.get('sequence','1')=='1':<br /> vals['sequence'] = self.pool.get('ir.sequence').get(cr, uid, 'sale.order.line') or '1'<br /> return super(sale_order_line, self).create(cr, uid, vals, context=context)
sale_view.xml<?xml version="1.0"?><br /><openerp><br /> <data><br /><br /> <record model="ir.ui.view" id="sale_order_form_add_line_seq"><br /> <field name="name">sale.odrer.form.add.line.seq</field><br /> <field name="model">sale.order</field><br /> <field name="inherit_id" ref="sale.view_order_form"/><br /> <field name="arch" type="xml"><br /> <br /> <xpath expr="//tree/field[@name='product_id']" position="before"><br /> <field name="sequence" string="序"/><br /> </xpath><br /><br /> </field><br /> </record><br /><br /> </data><br /></openerp><br />
sale_sequence.xml<?xml version="1.0" encoding="utf-8"?><br /><openerp><br /> <!-- <data noupdate="1"> --><br /> <data noupdate="1"><br /><br /> <!-- Sequences for sale.order.line --><br /> <record id="seq_type_sale_order_line" model="ir.sequence.type"><br /> <field name="name">Sales Order Line</field><br /> <field name="code">sale.order.line</field><br /> </record><br /><br /> <record id="seq_sale_order" model="ir.sequence"><br /> <field name="name">Sales Order Line</field><br /> <field name="code">sale.order.line</field><br /> <field name="prefix"></field><br /> <field name="padding">2</field><br /> </record><br /><br /> </data><br /></openerp>
以上实现了自增,但另外新建一个订单的时候,序号没法重置为1;
buke的ir_sequence_autoreset模块貌似只能按时间重置,不知道怎么按新订单重置line的序号...