1
我正在使用可以一般鏈接到幾種不同模型的投票模式(以允許您在不同的事情上進行投票)。在我的一些代碼中,我使用註釋來計算查詢集的總票數和正票數。我注意到,如果我對某個項目投票,那麼其他類型的具有相同ID的模型也會顯示它們已被投票(通過總票數和正面投票)。如果我查看未投票的對象的投票列表,它將返回一個空列表(但註釋仍然表示+1)。GenericRelation的註釋不正確
下面是一些代碼:
class UserVote():
user = models.ForeignKey(User)
vote = models.SmallIntegerField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
class Model1():
votes = generic.GenericRelation(UserVote)
class Model2():
votes = generic.GenericRelation(UserVote)
而且重現錯誤:
m1 = Model1()
m1.save()
m2 = Model2()
m2.save()
m1.votes.add(UserVote(user_id=1, vote=1))
Model2.objects.all().annotate(tot_votes=Count('votes__vote'))[0].tot_votes # Returns 1
Model2.objects.all().annotate(tot_votes=Count('votes__vote'))[0].votes.all() # Returns []
我不知道這是否是在註釋代碼中的錯誤,或者如果我失去了一些額外的有點區分不同的一般相關模型上的投票。我正在運行Django的主幹版本,順便說一句。
編輯: 看來有一個開放的錯誤(我想下次我會先搜索)在http://code.djangoproject.com/ticket/10461。我會留下這爲後人開放/也許有人可以找出解決方法。