[Answer]-Dictsort not working right with datetimes returned from model property

1👍

Finally figured out what was going on.

I’ve got Django set to timezone aware, so it was giving me time zone aware dates from the database when there was a meeting attached to the section, but when there was no such meeting, I was providing the date as:

start = datetime.datetime.utcnow()

which is timezone unaware.

Dictsort was silently failing when comparing time zone aware and time zone unaware dates.

Now that I’ve switched to providing a timezone aware date, it’s working:

start = datetime.datetime.utcnow().replace(tzinfo=utc)
👤jkjk

0👍

It looks like the implementation of the dictsort requires course.section_set.all to be an iterable.

However, if there aren’t any section attached to course, course.section_set.all would return None instead of an empty iterator, making course.section_set.all|dictsort fail.

You need to test course.section_set.count is postive and filter course.section_set.all conditionally.

Leave a comment