This question has been flagged
1 Reply
6913 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>

-->

<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)"/> -->

</xpath>

</field>

</record>


class purchase_order_line(osv.osv):

_inherit = 'purchase.order.line'

_columns={

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

}

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

values={}

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

values.update({'line_no':order.line_no+1})

return {'line_no':values}

Avatar
Discard
Author

*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