Tom*_*Tom 9 django orm annotations
鉴于这两个模型:
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)
我希望按总视图排序所有配置文件.我可以获得一个按总视图排序的配置文件ID列表:
ProfileView.objects.values('profile').annotate(Count('profile')).order_by('-profile__count')
但这只是一个配置文件ID的字典,这意味着我必须循环它并将一个配置文件对象列表放在一起.这是一些额外的查询,仍然不会导致QuerySet.那时,我不妨放弃原始SQL.在我这样做之前,有没有办法从Profile模型中做到这一点?ProfileViews通过ForeignKey字段相关,但它不像Profile模型那样知道,所以我不确定如何将两者结合在一起.
顺便说一句,我意识到我可以将视图存储为Profile模型中的属性,这可能会成为我在这里所做的事情,但我仍然有兴趣学习如何更好地使用聚合函数.
Bot*_*res 11
ProfileView通过ForeignKey字段相关,但它不像Profile模型那样知道
Profile模型确实知道这一点.你应该可以这样做:
  Profile.objects.all().annotate(count_views=Count('profileview__pk')).order_by('count_views')
编辑:在这里你可以找到关于跨越关系的查找的Django文档http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships
| 归档时间: | 
 | 
| 查看次数: | 2735 次 | 
| 最近记录: |