Django的聚集跨反向关系 [英] Django Aggregation Across Reverse Relationship

查看:172
本文介绍了Django的聚集跨反向关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于这两款车型:

 类档案(models.Model):
    用户= models.ForeignKey(用户,独特= TRUE,verbose_name = _('用户'))
    关于= models.TextField(_(关于),空白= TRUE)
    邮编= models.CharField(MAX_LENGTH = 10,verbose_name ='拉链code',空白= TRUE)
    网站= models.URLField(_('网站'),空白= TRUE,verify_exists = FALSE)类ProfileView(models.Model):
    简介= models.ForeignKey(简介)
    观众= models.ForeignKey(用户,空白= TRUE,空=真)
    创建= models.DateTimeField(auto_now_add = TRUE)

我想通过总的观点排序的所有配置文件。我可以通过总的观点与分类轮廓ID列表:

<$p$p><$c$c>ProfileView.objects.values('profile').annotate(Count('profile')).order_by('-profile__count')

但是,这只是一个配置文件ID的字典,这意味着以后我就遍历它放在一起轮廓对象的列表。这是许多附加的查询和仍然不会导致一个查询集。在这一点上,我还不如下降到原始的SQL。我这样做之前,有没有办法从剖面模型做到这一点? ProfileViews通过一个ForeignKey的领域有关,但好像剖面模型的人都知道它是没有,所以我不知道如何配合两个在一起。

顺便说一句,我知道我可以只保存看法的剖面模型的属性,并可能变成是什么,我在这里做的,但我仍然有兴趣学习如何更好地使用聚合函数。


解决方案

  

ProfileViews通过一个ForeignKey的领域有关,但好像剖面模型的知道它不是


配置文件模型实际上不知道。
你应该能够做到这样的:

  Profile.objects.all()。注释(count_views = COUNT('profileview__pk'))。ORDER_BY('count_views')

修改:在这里你可以找到有关跨越关系<一个查找Django文档href=\"http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships\">http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

Given these two models:

class Profile(models.Model):
    user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
    about = models.TextField(_('about'), blank=True)
    zip = models.CharField(max_length=10, verbose_name='zip code', blank=True)
    website = models.URLField(_('website'), blank=True, verify_exists=False)

class ProfileView(models.Model):
    profile = models.ForeignKey(Profile)
    viewer = models.ForeignKey(User, blank=True, null=True)
    created = models.DateTimeField(auto_now_add=True)

I want to get all profiles sorted by total views. I can get a list of profile ids sorted by total views with:

ProfileView.objects.values('profile').annotate(Count('profile')).order_by('-profile__count')

But that's just a dictionary of profile ids, which means I then have to loop over it and put together a list of profile objects. Which is a number of additional queries and still doesn't result in a QuerySet. At that point, I might as well drop to raw SQL. Before I do, is there a way to do this from the Profile model? ProfileViews are related via a ForeignKey field, but it's not as though the Profile model knows that, so I'm not sure how to tie the two together.

As an aside, I realize I could just store views as a property on the Profile model and that may turn out to be what I do here, but I'm still interested in learning how to better use the Aggregation functions.

解决方案

ProfileViews are related via a ForeignKey field, but it's not as though the Profile model knows that

The Profile model actually does know that. You should be able to do something like:

  Profile.objects.all().annotate(count_views=Count('profileview__pk')).order_by('count_views')

Edit: Here you can find Django docs about lookups that span relationships http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

这篇关于Django的聚集跨反向关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆