title: Django dictsort not working with date
body: <p>I have a simple pair of models for working with time series:</p>
  
  <pre><code>class Ticker(models.Model):
      name = CharField(max_length=100)
  
       def latest_datapoint(self):
           return self.datapoint_set.order_by('-timestamp')[0]
  
       def latest_value(self):
           return self.latest_data_point().value
  
       def latest_timestamp(self)
           return self.latest_data_point().timestamp
  
  class DataPoint(models.Model):
       ticker = ForeignKeyField(Ticker)
       timestamp = DateTimeField()
       value = FloatField()
  </code></pre>
  
  <p>I then use a generic class based view to make a ticker list:</p>
  
  <pre><code>class TickerListView(ListView):
      model = Ticker
      template_name = 'ticker-list.html'
  </code></pre>
  
  <p>Lastly, I implement the template:</p>
  
  <pre><code>&lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Latest Value&lt;/th&gt;
              &lt;th&gt;Latest Timestamp&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          {% for object in object_list %}  &lt;----***Let's talk about this***
              &lt;tr&gt;
                  &lt;td&gt;{{ object.name }}&lt;/td&gt;
                  &lt;td&gt;{{ object.latest_value}}&lt;/td&gt;
                  &lt;td&gt;{{ object.latest_timestamp}}&lt;/td&gt;
              &lt;/tr&gt;
          {% endfor % }
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>As written, this works just fine.  But I would like to order my results <strong>in the template</strong> using dictsort.  If I replace the highlighted line with <code>{% for object in object_list|dictsort:"latest_value" %}</code> everything works fine.  But if I instead write<code>{% for object in object_list|dictsort:"latest_timestamp" %}</code> everything comes in blank , as if I had specified an invalid key to sort on.  Any idea what's going on here?</p>
  
  <p>(Hypothesis: dictsort doesn't know how to handle <code>datetime.datetime</code> data types when ordering.) </p>
  
  <p>Thanks.</p>
  
  <h2>Added in response to Hieu Nguyen:</h2>
  
  <p>Thanks for your comment!  So of course I'm getting burned because I have oversimplified the example above.  In my actual code the method definitions for <code>Ticker</code> look like:</p>
  
  <pre><code>     def latest_datapoint(self):
           if self.datapoint_set:
               return self.datapoint_set.order_by('-timestamp')[0]
           else:
               return None
  
       def latest_value(self):
           if self.latest_datapoint():
               return self.latest_data_point().value
           else:
               return None
  
       def latest_timestamp(self)
           if self.latest_datapoint():             
               return self.latest_data_point().timestamp
           else:
               return None
  </code></pre>
  
  <p>When I try to dictsort on <code>latest_value</code> everything works fine, even if several of the Tickers have <code>latest_value() = None</code>.  But when I dictsort on <code>latest_timestamp</code> when some of those are <code>None</code> then Django seems to skip the table entirely.  Is the lesson here that <code>None</code> can be cleanly compared with Float data but not with DateTime data?  If so, Would a <code>latest_timestamp_text</code> method, which coerces all timestamps into a <code>YYYYMMDD HHMMSS</code> format (with <code>None</code> being converted to <code>0</code>) suffice as a key on which to perform in-template dictsorts?</p>
  
  <h2>Edited again:</h2>
  
  <p>This still doesn't seem to sort it out.  I have implemented a new method:</p>
  
  <pre><code>def latest_timestamp_safe(self):
      if self.latest_timestamp() is None:
          return datetime.now().replace(year=1975)
      else:
          return self.latest_timestamp()
  </code></pre>
  
  <p>I have confirmed that this is working and that all the rows in my table display a nice tz-aware datetime in the column I made for latest_timestamp_safe.  However I still cannot dictsort on this key.  Any thoughts?</p>
  
  <h2>Edit last time:</h2>
  
  <p>Above doesn't work.  As Hieu points out this doesn't suffice to make the "safe" datetime tz aware.  So I just gave up on that and went with the stringifying idea.  Works fine.  Compares with <code>None</code> in the expected way.</p>