1 回答

TA贡献1796条经验 获得超4个赞
对于给定的,Cinema您可以使用:
some_cinema.movie_set.all()
或者你可以给出ForeignKey一个更有意义的名字来反向查询:
class Movie(models.Model):
title = models.CharField(max_length=255)
description = models.CharField(max_length=255)
posting_cinema = models.ForeignKey(
'cinemas.Cinema',
on_delete=models.CASCADE,
null=True,
# here we give the reverse relation a name
related_name='movies'
)
然后你可以查询这个:
some_cinema.movies.all()
另一种方法是基于 过滤ForeignKey,例如:
Movie.objects.filter(posting_cinema=some_cinema)
或者如果你有电影院的主键:
Movie.objects.filter(posting_cinema__pk=some_cinema_pk)
(例如,这可以保存对Cinema对象的提取,如果您从不真正需要它Cinema本身)。
添加回答
举报