我有两个模型,一个是
class NewsLikes(models.Model):
id = models.IntegerField(db_column="id", max_length=11, help_text="")
userId = models.ForeignKey(Users, db_column='userId', max_length=11, help_text="")
newsId = models.ForeignKey(NewsMappings, db_column='newsMappingId', max_length=11, help_text="")
createdAt = models.DateTimeField(db_column='createdAt', auto_now=True, help_text="
,另一个模型是
class NewsMappings(models.Model):
id = models.IntegerField(db_column="id", max_length=11, help_text="")
newsId = models.ForeignKey(News, db_column='newsId', max_length=11, help_text="")
boardId = models.ForeignKey(Boards, db_column='boardId', max_length=11, help_text="")
isWallPost = models.BooleanField(db_column="isWallPost", default=False, help_text="")
模型新闻包含新闻映射的外键。我将新闻映射作为我的django模板中的上下文传递,我想要计算有多少新闻喜欢id在新闻映射方面存在并将其显示在模板上。
答案 0 :(得分:0)
Many to one有一个django文档 其中有外键关系映射解释。在您的情况下,如果您正在寻找每个NewsLikeID的所有NewsMappings的列表然后检查如下所示
NewsLikes.objects.filter(NewsMapping__newsId='SomeNewsID')