Odoo Help


How to add sequence number in purchase order line

on 4/16/15, 3:05 AM 2,556 views

------------------this is my code but its not working------------

<record model="ir.ui.view" id="view_for_four_task_form_new">

<field name="name">order.line</field>

<field name="model">purchase.order</field>

<field name="inherit_id" ref="purchase.purchase_order_form" />

<field name="arch" type="xml">

<!-- <xpath expr="/form/sheet/notebook/page/field/tree/field[@name='name']" position="after">

<field name="pdt_country"/>



<xpath expr="/form/sheet/notebook/page/field/tree/field[@name='name']" position="before">

<field name="line_no"/>

<!-- <field name="sequence_no" on_change="onchange_line(sequence_no)"/> -->




class purchase_order_line(osv.osv):

_inherit = 'purchase.order.line'


'line_no':fields.integer('Line Number')


def _get_seqeunces(self,cr,uid,ids,context=None):


for order in self.browse(cr, uid, ids, context=context):


return {'line_no':values}

*See revised answer below*. You need to also define the sort method in the Python code. class purchase_order_line(osv.osv): _inherit = 'purchase.order.line' _columns={ 'line_no': fields.integer('Line Number') } _order = 'line_no' From what i can see, your _get_sequences() function shouldn't be necessary.  Displaying the line_no field in the view isn't necessary either, unless you wanted to add the handle widget so that you can drag and drop lines to automatically re-sequence them.  Note that you don't need to detail the full XML path, you can use double forward slashes to indicate wildcards in xpath.  For example: order.line purchase.order <xpath expr="//field[@name='order_line']/tree/field[@name='name']" position="before"> *EDIT:* My mistake, I misinterpreted your question.  Based on what you've described, I'd accomplish that by using two fields, one for sequencing, and one for numbering.  You seemed to be on this track originally, I wasn't sure before why you had some commented out fields, but those make more sense now.  Line numbering can instead be a functional field rather than a discrete field so it can be easily recomputed on the fly.  Here's a rough, simple implementation with a few areas highlighted that could be improved upon: class purchase_order_line(osv.osv): _inherit = 'purchase.order.line' def _get_line_numbers(self, cr, uid, ids, fields, arg, context=None): """Assign sequential numbers to line items, starting at 1""" if context is None: context = {} res = {} line_num = 1 # Take the first line in the order, find the root PO, then iterate through all # PO line items to sequence them properly # # TODO: Better implementation would be to do a read_group or cr.execute() to find all POs # from the given line items, then sequence them all together but independently. That will # catch more edge case scenarios, though this will generally be sufficient for most uses. first_line_rec = self.browse(cr, uid, ids, context=context)[0] for line_rec in first_line_rec.order_id.order_line: res[line_rec.id] = line_num line_num += 1 return res _columns = { 'sequence': fields.integer('Sequence'), # TODO: Consider making this a stored field to save on read time 'line_no': fields.function(_get_line_numbers, type='integer', string='Line Number'), } _order = 'sequence' _defaults = { 'sequence': 10, } And the view definition: order.line purchase.order <xpath expr="//field[@name='order_line']/tree/field[@name='product_id']" position="before"> Here's what it looks like:  http://solarismed.com/po.jpg [1] [1] http://solarismed.com/po.jpg

Brett Lehrer
on 4/16/15, 7:43 PM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 4/16/15, 3:05 AM
Seen: 2556 times
Last updated: 4/29/15, 12:40 PM