Welcome to the Treehouse Community
Looking to learn something new?
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?
I'm assuming we'd use order_by method, as it was used in databases tutorial. So,
would sort with newest first, but we want the opposite.
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
"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 :(
Kenneth LoveTreehouse 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.
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
...since it would sort by ascending by default.
Using the following didn't work either:
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?