Skip to Content
Menu
This question has been flagged
2 Replies
7716 Views

Hi Sirs,

I'm trying to sort lines by a field in reports, but in rml i'm unable to get.

Any idea wellcoming,

Regards,

Avatar
Discard

First, the lines will be sorted by the model's _order attribute (which defaults to id) when it is browsed (or SELECT-ed). In order to change that, you need to either do a manual search and specify the order_by parameter or sort the records after the being selected. Maybe you can share the result that you needed and what is the result that you are getting so that we can help you further.

Best Answer

To sort invoice, you need a srto method in your rml parser... 
best way is to make a custom report parser... 
Here is an example parser :
 

class account_invoice(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(account_invoice, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'time': time,
            'sort':self._sort})

    def _sort(self, theList):
        #sorting by name ... change to apropriate: sequence, code price...
        theList.sort(key=lambda x: x.name, reverse=False)
        return theList

 

Now, in your rml you can instead of: [[ repeatIn(o.sort_print,'l') ]]

use :   [[ repeatIn(sort(o.invoice_line),'l') ]]
for reveserse sort order : [[ repeatIn(sort(o.invoice_line, reverse=True),'l') ]]
modifiy sort conditions to your needs...
hope it helps...

Avatar
Discard
Author

Hi sirs, I undestand that must to create a new app, isn't it? I tell you something.

yes, in order to do this you have to write the code somewhere, so best place is custom module...

Author Best Answer

Hi Ivan,

Thanks for your fast response, it is attached a DO that I want to sort by the 3rd field in column (rack). It seems like always it is sorted by description.

https://docs.google.com/a/onduex.com/file/d/0B0aJ9T6EXAWBeUtfNFRERnQxMnc/edit?usp=drivesdk

Avatar
Discard

Yes, DO lines (stock.move) are sorted by description by default. If you need to change the sort order, first, if the field is a function field or related field, it need to be stored in database. Then, @Bole has provided a very good example of a way to get it sorted without having to change the model's _order attribute (which will change all sorting order in the whole application for stock.move). The x.name in the _sort method should be replaced with the field that you want to sort with. If you want to change the model's sorting order in the whole application (mind you that stock.move is used for Incoming Shipment lines and Internal Move lines aside from Delivery Order lines), then you inherit the model can specify the _order attribute.

Sorry, to complete @Bole's example, the _sort method should accept reverse optional argument and pass it to the sort, i.e. def _sort(self, theList, reverse=False): #sorting by name ... change to apropriate: sequence, code price... theList.sort(key=lambda x: x.name, reverse=reverse) return theList