【发布时间】:2014-06-30 17:37:42
【问题描述】:
我有一个看起来像这样的:
class Post(models.Model):
user = models.ForeignKey(Profile, related_name='posts')
created = models.DateTimeField(auto_now_add=True)
description = models.CharField(max_length=100, null = True)
这有两个与之相关的模型:
class Comments(models.Model):
user = models.ForeignKey(Profile, related_name='author')
post = models.ForeignKey(Post, related_name='posts')
class Likes(models.Model):
user = models.ForeignKey(Profile, related_name='liker')
post = models.ForeignKey(Post, related_name='post' )
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('created',)
现在,逻辑基本上是计算喜欢的数量+随着时间的推移衰减的cmets数量:
likes + comments / now() - post.created
我已经创建了这个逻辑的 sql 版本,现在使用我创建的 django extra:
s = Post.objects.extras(select={'post':'logic here...... WHERE id = %d} % **MISSING PIECE THAT ALLOWS ME TO ITERATE OVER THE POST OBJECTS**)
谁能帮我弄清楚丢失的部分是什么?我尝试使用 Post.objects.all().id,它不起作用,因为它是一个查询集。
感谢任何帮助,cmets 了解更多信息
【问题讨论】:
标签: python sql django algorithm django-rest-framework