<p>最后我改变了我的方法:</p>
<p>我在<code>recipe</code>模型中创建了一个新的函数属性:</p>
<pre><code> @property
def get_comments_number(self):
api_key = settings.DISQUS_API_KEY
forum = settings.DISQUS_WEBSITE_SHORTNAME
url = 'https://disqus.com/api/3.0/forums/listThreads.json?api_key={0}&forum={1}&thread:ident={2}'.format(
api_key,
forum,
self.slug
)
raw = requests.get(url)
thread = json.loads(raw.content)
return thread['response'][0]['posts']
</code></pre>
<p>之后,我在模型中添加了一个新字段来保存<code>comment's number</code>值。你知道吗</p>
<p>最后,我在<code>DetailView</code>中调用该属性以更新注释的编号:</p>
<p>def调度(self、request、*args、**kwargs):</p>
<pre><code># TODO: maybe it is necessary create a task for update this in other cases
r = self.get_object()
r.comments = r.get_comments_number
r.save()
# check if the recipe is a private recipe if so through a 404 error
if r.shared == False and self.object.author != request.user:
output = _("Recipe {0} is marked Private").format(self.get_object().slug)
raise Http404(output)
else:
return super(RecipeDetailView, self).dispatch(request, *args, **kwargs)
</code></pre>
<p>现在我可以在另一个视图中按注释编号进行过滤:</p>
<pre><code>qs = qs.filter(shared=True).order_by('-comments')
</code></pre>
<p>就这样。你知道吗</p>