0
我有以下型號:ManyToManyField初始化在Django
class Comment(models.Model):
uuid = models.CharField(max_length=40, default='random')
news = models.ForeignKey('News')
user = models.OneToOneField(User, primary_key=True) # ForeignKey('User')
text = models.CharField(max_length=300, null=True)
upvotes = models.PositiveIntegerField(default=0)
downvotes = models.PositiveIntegerField(default=0)
votes = models.ManyToManyField(User, through='Vote', related_name='votes_table')
class Vote(models.Model):
cmt = models.ForeignKey(Comment)
user = models.ForeignKey(User)
vote_type = models.SmallIntegerField(default=0) # vote_type is +1 for upvote & -1 for downvote
當用戶提交一個新的評論,我初始化一個新的,像這樣的:c = Comment(uuid=uuid.uuid4(),news=n, user=request.user, text=comment_text)
。現在的問題是,當我打印c.votes.exists()
時,它是True
。這條評論如何在comments_votes
表中有條目?
您是否在保存新評論前檢查票是否存在? 當django處於DEBUG模型中時,您可以使用django.db中的 導入連接 print connection.queries [-1],您可以在django shell中看到生成的用於exists() –