See below:
On Wednesday, September 7, 2016 at 5:30:49 PM UTC-7, Peter wrote:
>
> My first question and I hope it's not a dumb one!
>
> My controller retrieves a number of rows from a table and I use *orderby=
> 'start_time' *to sort the list into date order.
> I can see this produces the desired result because when I output the rows
> to the log file the dates appear in the required order
>
> 'start_time': datetime.datetime(2016, 8, 1, 19, 0)
>> 'start_time': datetime.datetime(2016, 8, 8, 19, 0),
>> 'start_time': datetime.datetime(2016, 8, 15, 15, 0)
>> 'start_time': datetime.datetime(2016, 9, 6, 15, 52, 20)
>>
>
> The rows object is then passed to a view.html
>
>> return dict( inv_lines=inv_lines )* (along with two other objects
>> omitted for clarity)*
>>
>
> However in the view, looping through the view's inv_lines object the date
> order has changed
> and it produces this output...
>
> 'start_time': datetime.datetime(2016, 8, 1, 19, 0)
>> 'start_time': datetime.datetime(2016, 8, 8, 19, 0)
>> 'start_time': datetime.datetime(2016, 8, 15, 15, 0)
>> 'start_time': datetime.datetime(2016, 9, 6, 15, 52, 20)
>> 'start_time': datetime.datetime(2016, 8, 2, 16, 0)
>>
>
> I can see that the view is actually sorting the rows by id but I can't
> figure out how to override it?
>
> Is there an easy way to do so?
>
> Thanks!
>
What does your loop look like?
Python dictionaries do not guarantee any particular ordering. Python lists
do, and the DAL Rows object is a special subclass of a list,
and your inv_list should have the ordering that your query gave it, and the
dict you wrap it in should have inv_list as its first (and only) element,
and the view processor should preserve that to give you same order.
If you do (as in the book ... Chapter 6)
{{=inv_list}}
this will be translated to
{{=SQLTABLE(inv_list)}}
which should display things in the proper order.
An explicit for-loop (if you need finer layout control), it could look
something like
<ul>
{{for i in range(0,len(inv_list)):}}
{{=LI(inv_list[i])}}
{{pass}}
</ul>
which should be equivalent to
<ul>
{{for inv_item in inv_list:}}
{{=LI(inv_item)}}
{{pass}}
</ul>
If this doesn't answer your question, please post your view code so we can
give better answers.
/dps
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.