擅长:python、mysql、java
<p>我假设您也在其他地方使用<code>latest_entry()</code>,所以只需创建另一个方法<code>latest_added_time()</code>,它返回<code>latest_entry.added</code>或假时间。你知道吗</p>
<pre><code>class TopicCenter(models.Model):
...
def latest_added_time(self):
latest = self.latest_entry()
if latest:
return latest.added
else:
# returns a time to place it at the end of the sorted list
return datetime(1, 1, 1)
</code></pre>
<p>然后,您可以使用这种新方法进行排序:</p>
<pre><code>tcss = TopicCenter.objects.all().distinct('id')
sorter = lambda x: x.latest_added_time()
tcs = sorted(tcss, key=sorter, reverse=True)
</code></pre>
<p>如果您没有将<code>latest_entry()</code>用于其他任何东西,那么您应该将此逻辑直接放入该函数中。你知道吗</p>