Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Build a Social Network with Flask Making Strong Users Flask and Peewee Review

Need help on quiz question: how to sort entries based on published attribute?

So I tried this at least good 10-15 minutes no results.

QUESTION: In the following query, I want to change the sorting of my records. How would I sort them based on their published attribute so I get the oldest first?

Entry.select()._(___)

I'm assuming we'd use order_by method, as it was used in databases tutorial. So,

Entry.select().order_by(Entry.published.desc())

would sort with newest first, but we want the opposite.

Entry.select().order_by(Entry.published)

would sort with oldest first when tried as script. It's from official documentation reference, link: http://peewee.readthedocs.org/en/latest/peewee/querying.html#sorting-records

which I tried in singleplayer, I mean just as script and it worked but test doesn't want to accept this solution.

Another method I thought of was

Entry.select().order_by(-Entry.published.desc())

"You can also use + and - prefix operators to indicate ordering:" e.g. this should do the trick as well, which I try, but test doesn't this solution as well :(

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hey, sorry about this one. I based the answer off how we sorted records in the Peewee course where we by the name of an attribute instead of the attribute itself.

So the answer it was expecting was Entry.select().order_by('published'). I've updated it to allow the other version where you provide the field itself.

Thanks, it works! Nice)

I think I had the same issue, but can't remember what I did to pass the quiz... or if I just accepted that I wasn't going to get full marks on that one!

In my mind, it should just be

Entry.select().order_by(Entry.published)

...since it would sort by ascending by default.

Using the following didn't work either:

Entry.select().order_by(Entry.published.asc())

Entry.select().order_by(+Entry.published)

Note that in your last example you used by the minus sign and desc(), but I don't think you can use both (unless you were hoping to do some sort of reverse-reverse to get back to the ascending order?)

Kenneth Love, any ideas why this isn't working?

Yeah that's what I was thinking, yet after a large amount of tries I still get "wrong answer". Someone call the Kenneth Love!

sowmya c
sowmya c
6,966 Points

A small change is required

Entry.select().order_by(Entry.published)