7

我有一组表,其中包含用户创建和投票的内容。

内容_a

id         /* the id of the content */
user_id    /* the user that contributed the content */
content    /* the content */

内容_b

id
user_id
content

内容_c

id
user_id
content

投票

user_id         /* the user that made the vote */
content_id      /* the content the vote was made on */
content_type_id /* the content type the vote was made on */
vote            /* the value of the vote, either +1 or -1 */

我希望能够选择一组用户,并根据他们制作的内容的投票总和对他们进行排序。例如,

SELECT * FROM users ORDER BY <sum of votes on all content associated with user>

有没有一种特定的方法可以使用 Django 的 ORM 来实现,还是我必须使用原始 SQL 查询?在原始 SQL 中实现这一目标的最有效方法是什么?

4

3 回答 3

7

更新

假设模型是

from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType


class ContentA(models.Model):
    user = models.ForeignKey(User)
    content = models.TextField()

class ContentB(models.Model):
    user = models.ForeignKey(User)
    content = models.TextField()

class ContentC(models.Model):
    user = models.ForeignKey(User)
    content = models.TextField()

class GenericVote(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()
    user = models.ForeignKey(User)
    vote = models.IntegerField(default=1)

选项 A. 使用GenericVote

GenericVote.objects.extra(select={'uid':"""
CASE
WHEN content_type_id = {ct_a} THEN (SELECT user_id FROM {ContentA._meta.db_table} WHERE id = object_id)
WHEN content_type_id = {ct_b} THEN (SELECT user_id FROM {ContentB._meta.db_table} WHERE id = object_id)
WHEN content_type_id = {ct_c} THEN (SELECT user_id FROM {ContentC._meta.db_table} WHERE id = object_id)
END""".format(
ct_a=ContentType.objects.get_for_model(ContentA).pk,
ct_b=ContentType.objects.get_for_model(ContentB).pk,
ct_c=ContentType.objects.get_for_model(ContentC).pk,
ContentA=ContentA,
ContentB=ContentB,
ContentC=ContentC
)}).values('uid').annotate(vc=models.Sum('vote')).order_by('-vc')

上面的ValuesQuerySet,(或 use values_list())为您提供了 s 的 ID 序列User(),按票数降序排列。然后,您可以使用它来获取顶级用户。

选项 B. 使用User.objects.raw

当我使用时User.objects.raw,我得到了几乎相同的查询 w/ forsvarir 给出的答案

User.objects.raw("""
SELECT "{user_tbl}".*, SUM("gv"."vc") as vote_count from {user_tbl},
    (SELECT id, user_id, {ct_a} AS ct FROM {ContentA._meta.db_table} UNION
     SELECT id, user_id, {ct_b} AS ct FROM {ContentB._meta.db_table} UNION
     SELECT id, user_id, {ct_c} as ct FROM {ContentC._meta.db_table}
    ) as c,
   (SELECT content_type_id, object_id, SUM("vote") as vc FROM {GenericVote._meta.db_table} GROUP BY content_type_id, object_id) as gv
WHERE {user_tbl}.id = c.user_id
    AND gv.content_type_id = c.ct
    AND gv.object_id = c.id
GROUP BY {user_tbl}.id
ORDER BY "vc" DESC""".format(
    user_tbl=User._meta.db_table, ContentA=ContentA, ContentB=ContentB,
    ContentC=ContentC, GenericVote=GenericVote, 
    ct_a=ContentType.objects.get_for_model(ContentA).pk,
    ct_b=ContentType.objects.get_for_model(ContentB).pk,
    ct_c=ContentType.objects.get_for_model(ContentC).pk
))

选项 C. 其他可能的方式

  • 根据 Michael Dunn 的建议vote_count,对模型进行反规范化User或剖析模型,例如UserProfile或其他相关模型。如果您经常即时访问,这会表现得更好。vote_count
  • 构建一个UNION为您执行 s 的数据库视图,然后将模型映射到它,这可以使查询的构建更容易。
  • 在 Python 中进行排序,通常是处理大规模数据的最佳方式,因为有数十种工具包和扩展方式。

在使用 Django ORM 进行查询之前,您需要一些 Django 模型来映射这些表。假设它们是匹配和表的模型User,那么您可以Votingusersvoting

User.objects.annotate(v=models.Sum('voting__vote')).order_by('v')
于 2012-05-20T15:14:04.610 回答
3

对于原始 SQL 解决方案,我在这里在 ideone上创建了您的问题的粗略复制

数据设置:

create table content_a(id int, user_id int, content varchar(20));
create table content_b(id int, user_id int, content varchar(20));
create table content_c(id int, user_id int, content varchar(20));
create table voting(user_id int, content_id int, content_type_id int, vote int);
create table users(id int, name varchar(20));
insert into content_a values(1,1,'aaaa');
insert into content_a values(2,1,'bbbb');
insert into content_a values(3,1,'cccc');
insert into content_b values(1,2,'dddd');
insert into content_b values(2,2,'eeee');
insert into content_b values(3,2,'ffff');
insert into content_c values(1,1,'gggg');
insert into content_c values(2,2,'hhhh');
insert into content_c values(3,3,'iiii');
insert into users values(1, 'first');
insert into users values(2, 'second');
insert into users values(3, 'third');
insert into users values(4, 'voteonly');

-- user 1 net votes (2)
insert into voting values (1, 1, 1, 1);
insert into voting values (2, 3, 1, -1);
insert into voting values (3, 1, 1, 1); 
insert into voting values (4, 2, 1, 1); 

-- user 2 net votes (3)
insert into voting values (1, 2, 2, 1);
insert into voting values (1, 1, 2, 1);
insert into voting values (2, 3, 2, -1);
insert into voting values (4, 2, 2, 1);
insert into voting values (4, 2, 3, 1);

-- user 3 net votes (-1)
insert into voting values (2, 3, 3, -1);

我基本上假设 content_a 的类型为 1,content_b 的类型为 2,content_c 的类型为 3。使用原始 SQL,似乎有两种明显的方法。首先是将所有内容联合在一起,然后将其与用户和投票表连接起来。我在下面测试了这种方法。

select users.*, sum(voting.vote)
from users, 
    voting, (
        SELECT     id, 1 AS content_type_id, user_id
        FROM         content_a
        UNION
        SELECT     id, 2 AS content_type_id, user_id
        FROM         content_b
        UNION
        SELECT     id, 3 AS content_type_id, user_id
        FROM         content_c) contents
where contents.user_id = users.id
and voting.content_id = contents.id
and voting.content_type_id = contents.content_type_id
group by users.id
order by sum(voting.vote) desc;

另一种方法似乎是将内容表外部连接到投票表,而不需要联合步骤。这可能性能更高,但我无法测试它,因为 Visual Studio 一直在为我重写我的 sql ......我希望 SQL 看起来像这样(但我还没有测试过):

select users.*, sum(voting.vote)
from users, voting, content_a, content_b, content_c
where users.id = content_a.user_id (+)
and users.id = content_b.user_id (+)
and users.id = content_c.user_id (+)
and ((content_a.id = voting.content_id and voting.content_type_id = 1) OR
     (content_b.id = voting.content_id and voting.content_type_id = 2) OR
     (content_c.id = voting.content_id and voting.content_type_id = 3))
group by users.id
order by sum(voting.vote) desc;
于 2012-05-29T07:41:13.843 回答
0

我会使用预先计算的值来做到这一点。首先制作一个单独的表来存储每个用户收到的投票:

class VotesReceived(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    count = models.IntegerField(default=0, editable=False)

然后在每次投票时使用 post_save信号更新计数:

def update_votes_received(sender, instance, **kwargs):
    # `instance` is a Voting object
    # assuming here that `instance.content.user` is the creator of the content
    vr, _ = VotesReceived.objects.get_or_create(user=instance.content.user)
    # you should recount the votes here rather than just incrementing the count
    vr.count += 1 
    vr.save()

models.signals.post_save.connect(update_votes_received, sender=Voting)

用法:

user = User.objects.get(id=1)
print user.votesreceived.count

如果您的数据库中已经有数据,您当然必须在第一次手动更新投票计数。

于 2012-05-29T08:28:21.727 回答