Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

如何实现sale.order.line中的sequence字段自增?



  • OpenERP 7.0
    sale.order.line中的sequence字段默认值为10,通过widget="handle"显示在界面上,而不是直接的数字

    <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tree string=&quot;Sales Order Lines&quot; editable=&quot;bottom&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;sequence&quot; widget=&quot;handle&quot;/&gt;<br />
    



    现在想要将sequence再次显示在订单行中,
    视图部分简单:

    <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;xpath expr=&quot;//tree/field[@name=&#039;product_id&#039;]&quot; position=&quot;before&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;sequence&quot; string=&quot;序号&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/xpath&gt;<br />
    


    但希望每个订单中,sequence字段从1开始自动增号,并且添加一个订单明细时,如果取消当前添加项目后,再添加新项目时不跳号,如何实现呢?

    试了shelly神的改代码打法, http://shine-it.net/index.php?topic=2089.0
    '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 />&nbsp; &nbsp;  _defaults = {<br />&nbsp; &nbsp; &nbsp; &nbsp;  &#039;sequence&#039;: &#039;/&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; def create(self, cr, uid, vals, context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; if vals.get(&#039;sequence&#039;,&#039;/&#039;)==&#039;/&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vals&#91;&#039;sequence&#039;] = self.pool.get(&#039;ir.sequence&#039;).get(cr, uid, &#039;sale.order.line&#039;) or &#039;/&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; return super(sale_order_line, self).create(cr, uid, vals, context=context)<br />
    


  • ValueError: invalid literal for int() with base 10: &#039;/&#039;<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 />&nbsp; &nbsp; _inherit = &quot;sale.order.line&quot;<br /><br />&nbsp; &nbsp; _defaults = {<br />&nbsp; &nbsp; &nbsp; &nbsp;  &#039;sequence&#039;: &#039;1&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; def create(self, cr, uid, vals, context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; if vals.get(&#039;sequence&#039;,&#039;1&#039;)==&#039;1&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vals&#91;&#039;sequence&#039;] = self.pool.get(&#039;ir.sequence&#039;).get(cr, uid, &#039;sale.order.line&#039;) or &#039;1&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; return super(sale_order_line, self).create(cr, uid, vals, context=context)&nbsp; &nbsp;  
    



    sale_view.xml

    &lt;?xml version=&quot;1.0&quot;?&gt;<br />&lt;openerp&gt;<br />&nbsp; &nbsp; &lt;data&gt;<br /><br />&nbsp; &nbsp; &nbsp; &lt;record model=&quot;ir.ui.view&quot; id=&quot;sale_order_form_add_line_seq&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;name&quot;&gt;sale.odrer.form.add.line.seq&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;model&quot;&gt;sale.order&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;inherit_id&quot; ref=&quot;sale.view_order_form&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;arch&quot; type=&quot;xml&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;xpath expr=&quot;//tree/field[@name=&#039;product_id&#039;]&quot; position=&quot;before&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;sequence&quot; string=&quot;序&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/xpath&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &lt;/record&gt;<br /><br />&nbsp; &nbsp; &lt;/data&gt;<br />&lt;/openerp&gt;<br />
    


    sale_sequence.xml

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />&lt;openerp&gt;<br />&nbsp; &nbsp; &lt;!-- &lt;data noupdate=&quot;1&quot;&gt; --&gt;<br />&nbsp; &nbsp; &lt;data noupdate=&quot;1&quot;&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- Sequences for sale.order.line --&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;record id=&quot;seq_type_sale_order_line&quot; model=&quot;ir.sequence.type&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;name&quot;&gt;Sales Order Line&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;code&quot;&gt;sale.order.line&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;/record&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;record id=&quot;seq_sale_order&quot; model=&quot;ir.sequence&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;name&quot;&gt;Sales Order Line&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;code&quot;&gt;sale.order.line&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;prefix&quot;&gt;&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;padding&quot;&gt;2&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;/record&gt;<br /><br />&nbsp; &nbsp; &lt;/data&gt;<br />&lt;/openerp&gt;
    



    以上实现了自增,但另外新建一个订单的时候,序号没法重置为1;

    buke的ir_sequence_autoreset模块貌似只能按时间重置,不知道怎么按新订单重置line的序号...



  • 其实, 没有必要自增... 可以制完单后, 手工调整一下顺序即可.


    参见  ir.ui.menu  对象的手动调整顺序后的序号自编相关代码.



  • 自增主要是为了方便统计个数,对于经常5个以上的产品输入就很有必要了。
    最好是这个字段能实时显示,存不存储在数据库中到无所谓...就像rml报表中的seq标签 [检测到链接无效,已移除]



  • 你这个直接写个模块. 使用JS 部分就可以搞定的...

    有预算直接用RMB 砸向 @广州-步科