English 中文(简体)
2. 通过评论将条目与django合并
原标题:Ordering entries via comment count with django

I need to get entries from database with counts of comments. Can i do it with django s comment framework? I am also using a voting application which is not using GenericForeignKeys i get entries with scores like this:

class EntryManager(models.ModelManager):
    def get_queryset(self):
        return super(EntryManager,self).get_queryset(self).all().annotate(
            score=Sum("linkvote__value"))

但是,当有外国骑师时,我就被 st。 你们是否对此有任何想法?

extra explaination: i need to fetch entries like this:

id | body | vote_score | comment_score |
 1 |  foo |         13 |             4 |
 2 |  bar |          4 |             1 |

在这样做之后,即可以通过评论命令他们:

Thans for all replies.

最佳回答

很显然,注意到逆向一般关系(或一般外过滤器)仍然是 开放票。 (另见corresponding documentation)。 直到这个问题得到解决,我建议使用extra/中的“ raw”。 问:

return super(EntryManager,self).get_queryset(self).all().annotate(
    vote_score=Sum("linkvote__value")).extra(select={
         comment_score : """SELECT COUNT(*) FROM comments_comment
            WHERE comments_comment.object_pk = yourapp_entry.id
            AND comments_comment.content_type = %s"""
    }, select_params=(entry_type,))

当然,你必须填写正确的表格名称。 此外,<代码> 入门_打字/代码>是一种“固定”,可在你的看管职能之外设置(见。 内容提要:

from django.contrib.contenttypes.models import ContentType
entry_type = ContentType.objects.get_for_model(Entry)

假设你有一个单一的模型<代码>。 页: 1 否则,事情会变得更加复杂:你需要一个分局,以标明每个附加说明的物体的类型。

问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...