[PonyORM-list] Problem with iterated linked table

Matthew Bell matthewrobertbell at gmail.com
Thu Nov 20 14:39:38 UTC 2014


so add a method to your post model, like:

def ordered_comments(self):
    return select(c for c in Comment if c.post == self).order_by(Comment.id)

then do {% for comment in post.ordered_comments() %}

The post.comments is a "set", so it isn't ordered.

PS: I don't think you need "{% if loop.index > numberComments %}{% break
%}{% endif %} "

On 20 November 2014 13:08, 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
>
>


-- 
Regards,

Matthew Bell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20141120/534b5d53/attachment-0001.html>


More information about the ponyorm-list mailing list