编辑:以下两个代码段实际上都运行正常,没有任何错误!在我的代码库中,这是一个错字。
有没有办法编写ORM调用,以便我可以获取ManyToMany对象的计数注释并预取这些对象?
e.g。
movies = Movies.objects.filter()
movies = movies.annotate(genre_count=Count('genre'))
movies = movies.prefetch_related('genre_set')
for movie in movies:
genres = movie.genre_set.all()
genre_count = movie.genre_count
抛出AttributeError:'Movie'对象没有属性'genre_count',而
movies = Movies.objects.filter()
movies = movies.annotate(genre_count=Count('genre'))
for movie in movies:
genre_count = movie.genre_count
工作正常