Skip to content

Instantly share code, notes, and snippets.

@gamesbrainiac
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamesbrainiac/5eacc2a5c1b0fb1cd52f to your computer and use it in GitHub Desktop.
Save gamesbrainiac/5eacc2a5c1b0fb1cd52f to your computer and use it in GitHub Desktop.
How to sort comments in Pony
# encoding=utf-8
__author__ = "Quazi Nafiul Islam"
from pony import orm
# orm.sql_debug(True)
db = orm.Database()
db.bind('sqlite', 'todo_api.db', create_db=True)
class Post(db.Entity):
name = orm.Required(unicode)
comments = orm.Set("Comment")
class Comment(db.Entity):
entry = orm.Required(unicode)
post = orm.Required(Post)
db.generate_mapping(create_tables=True)
with orm.db_session:
p = Post(name="Cheese")
comm1 = Comment(entry="Dude, your post sucks", post=p)
comm2 = Comment(entry="No, this post is awesome", post=p)
comm3 = Comment(entry="Who died and made you king?", post=p)
p2 = Post(name="I love Pony")
comm7 = Comment(entry="Dude, your post sucks", post=p2)
comm21 = Comment(entry="No, this post is awesome", post=p2)
comm77 = Comment(entry="Who died and made you king?", post=p2)
with orm.db_session:
print {
p.name: sorted(p.comments, key=lambda x: x.id) for p in Post.select()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment