1

I have a Word model, it has many to many relation with Phrase model I want to get rid of this loop because it's not effective in terms of performance:

for w in words:
    w.freq = w.phrases.count()
    w.save()

I know I should use annotate and subquery, I have found this question Django: how to annotate queryset with count of filtered ForeignKey field? But don't really understand how I should do it in my case

user2950593
  • 9,233
  • 15
  • 67
  • 131
  • Possible Duplicate https://stackoverflow.com/questions/30449960/django-save-vs-update-to-update-the-database – Ty C Nov 13 '18 at 23:03
  • @TyC If you give a try and read my question just a little bit more carefully, you will see, that it doesn't have anything regarding update vs save but The question was about the loop – user2950593 Nov 14 '18 at 00:19

0 Answers0