[PonyORM-list] Problem with iterated linked table
Alexander Kozlovsky
alexander.kozlovsky at gmail.com
Thu Nov 20 17:04:45 UTC 2014
Hi guys!
It seems that we should add several convenience methods to collections, so
it will be possible to do any of the following:
{% for comment in post.comments.order_by(Comment.id) %}
{% for comment in post.comments.filter(lambda c: c.date >= yesterday
).order_by(lambda c: desc(c.date)) %}
All of this can be achieved today, but with more complex syntax.
Also, we probably can automatically sort collection items by primary key
during iteration, if no explicit ordering was set. In some cases the
ordering is unnecessary, because conceptually the collection is just an
unordered set. But most users probably expect that the result of collection
iteration is predictable.
> IMO, the database should be doing the sorting in the query, not sorted()
Actually I prefer to do sorting in memory (if we need to process entire
collection), because it puts less burden on the database, and the
application code can be easily scaled by running multiple parallel
processes. But if only a subset of records is retrieved from the database,
then sorting should be done in the query, in order to avoid fetching of
unnecessary data. So maybe it will make sense to add two separate methods:
`post.comments.sort_by(Comment.id)` to do sorting in memory and
`post.comments.order_by(Comment.id).page(1, pagesize=10)` to add ORDER
BY...LIMIT sections to the query.
I hope that we can add `order_by` and `filter` methods to collections in
our next release )
On Thu, Nov 20, 2014 at 6:54 PM, Matthew Bell <matthewrobertbell at gmail.com>
wrote:
> IMO, the database should be doing the sorting in the query, not sorted()
>
> On 20 November 2014 15:16, Gamesbrainiac <gamesbrainiac at gmail.com> wrote:
>
>> Well, I have set up the following example for you, see if it works for
>> you:
>>
>> https://gist.github.com/gamesbrainiac/5eacc2a5c1b0fb1cd52f
>>
>> Hope this helps :)
>>
>> On Thu, Nov 20, 2014 at 7:08 PM, Pedro Baumann <ondoheer at gmail.com>
>> wrote:
>>
>>> Precisely! That's my problem.
>>> On 20 Nov 2014 02:21, "Gamesbrainiac" <gamesbrainiac at gmail.com> wrote:
>>>
>>>> I think I get your problem now. Right now, it is ordering by post ID.
>>>> Do you want the comments of a particular post to be ordered by ID, or do
>>>> you want something like:
>>>>
>>>> Post 1:
>>>> Comment 1
>>>> Comment 2
>>>> Post 2:
>>>> Comment 1
>>>> Comment 2
>>>>
>>>> ?
>>>>
>>>> On Thu, Nov 20, 2014 at 12:19 PM, Pedro Baumann <ondoheer at gmail.com>
>>>> wrote:
>>>>
>>>>> Hello Gamebrainiac
>>>>>
>>>>> Thanks for the link, it seems really interesting.
>>>>>
>>>>> I have tried both your suggestions and both gave mme the same problem.
>>>>>
>>>>> They both return the Post object ordered by id, same as my code did.
>>>>> The problem is that somehow when I iterate through the posts like soÑ
>>>>>
>>>>> {% for post in posts %}
>>>>> {% for comment in post.comments %}
>>>>>
>>>>>
>>>>> the comments come out in any order, and they change with every
>>>>> request!!!!
>>>>>
>>>>> That same thing happens with your suggestions.
>>>>> I think this hasn't happend to me with pony before, but somehow it
>>>>> does now. Is there a way to sort the linked table from jinja?
>>>>>
>>>>> something like post.comments.order_by(id)?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> *Pedro Baumann Cornejo*
>>>>> Psicólogo - Psicoterapeuta - Flores de Bach
>>>>> Francisco de Paula Ugarriza 309 dpto. 102 Miraflores.
>>>>> Lima
>>>>> Perú
>>>>>
>>>>> On Thu, Nov 20, 2014 at 1:02 AM, Gamesbrainiac <
>>>>> gamesbrainiac at gmail.com> wrote:
>>>>>
>>>>>> In addendum, this is the exact part where I talk about querying:
>>>>>> http://nafiulis.me/a-todo-app-with-flask-and-pony.html#id4
>>>>>>
>>>>>> On Thu, Nov 20, 2014 at 12:01 PM, Gamesbrainiac <
>>>>>> gamesbrainiac at gmail.com> wrote:
>>>>>>
>>>>>>> Try using:
>>>>>>>
>>>>>>> '"SELECT * FROM "post" ORDER BY "post"."id"'
>>>>>>>
>>>>>>> If I were you, I'd do the following in pony:
>>>>>>>
>>>>>>> Post.select().order_by(Post.id)
>>>>>>>
>>>>>>> Also, my post on Pony might be worth a look ->
>>>>>>> http://nafiulis.me/a-todo-app-with-flask-and-pony.html
>>>>>>>
>>>>>>> On Thu, Nov 20, 2014 at 11:43 AM, Pedro Baumann <ondoheer at gmail.com>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> Hello,
>>>>>>>>
>>>>>>>> first of all I want to thank you for creating such an intuitive and
>>>>>>>> pythonic ORM, I have enjoyed every minute if used it, it's most intuitive
>>>>>>>> and it really heps code readability.
>>>>>>>>
>>>>>>>> I'm having a small problem with my latest project, I hope you can
>>>>>>>> help me.
>>>>>>>>
>>>>>>>> I'm developing a web app that has a basic "blog like" module.
>>>>>>>>
>>>>>>>> Post->Comments->Replies
>>>>>>>>
>>>>>>>> I'm using Flask as a framework.
>>>>>>>>
>>>>>>>> I'm passing
>>>>>>>>
>>>>>>>>
>>>>>>>> *posts = Post.select_by_sql('SELECT * FROM post ORDER BY id')*
>>>>>>>>
>>>>>>>> to the template, and then iterating through it to print the
>>>>>>>> comments as in
>>>>>>>>
>>>>>>>> {% for comment in post.comments %}
>>>>>>>> {% if loop.index > numberComments %}{% break %}{% endif %}
>>>>>>>> // code code code (HTML)
>>>>>>>> {% endfor %}
>>>>>>>>
>>>>>>>> the problem is the comments come out in random order, varying on
>>>>>>>> each iteration.
>>>>>>>>
>>>>>>>> I need to be able to print the comments in order for my ajax
>>>>>>>> functions that retrieve more comments to work.
>>>>>>>>
>>>>>>>> I think I am missing something here.
>>>>>>>>
>>>>>>>> Thanks !
>>>>>>>>
>>>>>>>> *Pedro Baumann Cornejo*
>>>>>>>> Psicólogo - Psicoterapeuta - Flores de Bach
>>>>>>>> Francisco de Paula Ugarriza 309 dpto. 102 Miraflores.
>>>>>>>> Lima
>>>>>>>> Perú
>>>>>>>>
>>>>>>>> _______________________________________________
>>>>>>>> ponyorm-list mailing list
>>>>>>>> ponyorm-list at ponyorm.com
>>>>>>>> /ponyorm-list
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Kind Regards,
>>>>>>>
>>>>>>> Quazi Nafiul Islam
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Kind Regards,
>>>>>>
>>>>>> Quazi Nafiul Islam
>>>>>>
>>>>>> _______________________________________________
>>>>>> ponyorm-list mailing list
>>>>>> ponyorm-list at ponyorm.com
>>>>>> /ponyorm-list
>>>>>>
>>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> ponyorm-list mailing list
>>>>> ponyorm-list at ponyorm.com
>>>>> /ponyorm-list
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Kind Regards,
>>>>
>>>> Quazi Nafiul Islam
>>>>
>>>> _______________________________________________
>>>> ponyorm-list mailing list
>>>> ponyorm-list at ponyorm.com
>>>> /ponyorm-list
>>>>
>>>>
>>> _______________________________________________
>>> ponyorm-list mailing list
>>> ponyorm-list at ponyorm.com
>>> /ponyorm-list
>>>
>>>
>>
>>
>> --
>> Kind Regards,
>>
>> Quazi Nafiul Islam
>>
>> _______________________________________________
>> ponyorm-list mailing list
>> ponyorm-list at ponyorm.com
>> /ponyorm-list
>>
>>
>
>
> --
> Regards,
>
> Matthew Bell
>
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20141120/1abd4c79/attachment-0001.html>
More information about the ponyorm-list
mailing list