我有一個投票系統,用戶可以在帖子上投下或投票。投票將用於計算,所以我需要以日誌格式存儲它們,即我將每張投票保存在自己的表中。查詢Django每個用戶每個帖子的最新投票
事情是這樣的:
class PointLog(models.Model):
post = models.ForeignKey(Post, db_index=True)
points = models.IntegerField(db_index=True)
user = models.ForeignKey(User, blank=True, null=True)
time = models.DateTimeField(auto_now_add=True, db_index=True)
data = models.IntegerField() # -1, 0 or 1
現在我需要顯示20個職位,其中最後一投的用戶一起做。
我使用django-rest-framework
,所以我可以使用看起來像這樣的序列化程序字段; uservote = serializers.SerializerMethodField()
,再加上像一個函數:
def get_uservote(self, obj):
user = self.context['request'].user
vote = PointLog.objects.only('data').filter(user=user, post=obj).last()
return vote.data if vote else 0
但會做每一個後DB-查詢1次,我希望有一個更好的解決方案。
通過將queryset保存在self.context中,每次運行get_uservote時我都可以保存一個db-query,以便覆蓋該部分。 但我怎麼能做一個查詢,基於項目列表返回所有latest
數據從另一個表。
一開始就是PointLog.objects.filter(user=user, post__in=posts)
,但接下來呢?這甚至有可能在1查詢中使用原始SQL?
更新1: PointLog.objects.filter(...).order_by('post__id').distinct('post__id')
還挺會做,但我不認爲我保證能得到newest
投票。如果我使用order_by('pk')(或'time'),我不能使用distinct('post__id')
,因爲我將得到一個sql錯誤(ProgrammingError: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
)