Django按最新评级列出所有作者 [英] Django list all authors by most recent rating

查看:171
本文介绍了Django按最新评级列出所有作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于Django文档中的以下简化模型,我想返回所有作者的列表,按照最近条目的评级进行分组,也可以返回过去某个日期之前的最新版本。

 类作者(models.Model):
name = models.CharField(max_length = 50)
email = models .EmailField()

class Entry(models.Model):
headline = models.CharField(max_length = 255)
pub_date = models.DateTimeField()
mod_date = models.DateTimeField()
authors = models.ForeignKey(作者)
rating = models.IntegerField()

最后我想把它变成一个像Python这样的Python字典:{1star:(author1,author2),2star:(author3,author4,author5)...}。

$ b一个想法是返回所有条目,然后使用itertools.groupby来操纵大数据集。任何人都可以建议一个更干净的选择?

解决方案

另一种方法



  from collections import defaultdict 
from datetime import datetime,timedelta

week_ago = datetime.now() - timedelta(days = 7)

author_recent_ratings = dict(Entry.objects.filter(pub_date__gt = week_ago)
.order_by('pub_date')
.select_related()
.values_list('author','rating') )

recent_by_rating = defaultdict(list)
作者,评级在author_recent_ratings.iteritems():
recent_by_rating [rating] .append(author)

这是您可以做的一种方式。基本上,您通过最近的条目(在这种情况下从最后一周的条目)订购,然后按最早的顺序排序,然后将值列表返回的列表转换为dict。会发生什么事情,因为它被转换成一个dict,而较新的条目会破坏旧的条目,所以你最终会得到一个作者作为关键字的dict,他们的评分是值。


Given the following simplified models from the Django docs, I would like to return a list of all of the authors grouped by the rating on their most recent entry, or the most recent prior to some date in the past.

 class Author(models.Model):
     name = models.CharField(max_length=50)
     email = models.EmailField()

 class Entry(models.Model):
     headline = models.CharField(max_length=255)
     pub_date = models.DateTimeField()
     mod_date = models.DateTimeField()
     authors = models.ForeignKey(Author)
     rating = models.IntegerField()

Eventually I would like to turn this into a python dictionary like: {1star:(author1,author2),2star:(author3,author4,author5)...}.

One thought is to return all of the entries, then use itertools.groupby to manipulate the large dataset. Can anyone suggest a cleaner alternative?

解决方案

Another approach

from collections import defaultdict
from datetime import datetime, timedelta

week_ago = datetime.now() - timedelta(days=7)

author_recent_ratings = dict(Entry.objects.filter(pub_date__gt=week_ago)
                                          .order_by('pub_date')
                                          .select_related()
                                          .values_list('author', 'rating'))

recent_by_rating = defaultdict(list)
for author, rating in author_recent_ratings.iteritems():
    recent_by_rating[rating].append(author)

This is one way you could do it. Basically you order by most recent entries (in this case entries from the last week) then order by oldest first, and then you convert the list returned by values list into a dict. What happens is, as it's converted into a dict and the newer entries clobber the older ones so you end up with a dict that has authors as keys, with their ratings as the values.

这篇关于Django按最新评级列出所有作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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