在django中按最独特的视图(存储在另一个具有ip的表中)对对象进行排序
相关模型:
models.py
class Entry(models.Model):
...
class EntryView(models.Model):
entry = models.ForeignKey(Entry)
dt_clicked = models.DateTimeField(auto_now_add=True)
ip = models.CharField(max_length=15, db_index=True, blank=True)
host = models.CharField(max_length=64, db_index=True, blank=True)
referer = models.CharField(max_length=64, db_index=True, blank=True)
def __unicode__(self):
return "%s -> %s" % (self.ip, self.entry.headline)
我想我可以做这样的事情:
views.py
...
popular = Entry.articles.values('ip').annotate(Count("entry_views__id")).order_by()
...
但我看不出这是如何工作的,因为 Entry 中没有 EntryView 的字段。
Related models:
models.py
class Entry(models.Model):
...
class EntryView(models.Model):
entry = models.ForeignKey(Entry)
dt_clicked = models.DateTimeField(auto_now_add=True)
ip = models.CharField(max_length=15, db_index=True, blank=True)
host = models.CharField(max_length=64, db_index=True, blank=True)
referer = models.CharField(max_length=64, db_index=True, blank=True)
def __unicode__(self):
return "%s -> %s" % (self.ip, self.entry.headline)
I guess I could do something like this:
views.py
...
popular = Entry.articles.values('ip').annotate(Count("entry_views__id")).order_by()
...
But I can't see how this would work since there is no field for EntryView in Entry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下操作:
我使用您上面提供的两个模型对此进行了测试。这将返回一个
Entry
实例列表,其中每个实例都标注有EntryView.ip
计数,并按计数降序排列(最流行的在前)。You can do the following:
I tested this using the two models you gave above. This will return a list of
Entry
instances annotated with theEntryView.ip
count for each instance, ordered in the descending order of count (most popular first).