title: Django 2.1+ bulk update records with the count of their related records?
body: <p>I'm trying to bulk update all of my records in table A with the count of their related records in table B.</p>
  
  <p>I'd like to do something like:</p>
  
  <pre><code>from django.db.models import Subquery, OuterRef, Count
  
  
  table_b_subquery = TableB.objects.filter(a_id=OuterRef('id'))
  
  TableA.objects.all().update(table_b_count=Count(Subquery(table_b_subquery)))
  </code></pre>
  
  <p>That would be the equivalent of this NON-BULK method:</p>
  
  <pre><code># Non-Bulk
  
  for record in TableA.objects.all():
    record.table_b_count = record.table_b_set.count()
  </code></pre>
  
  <p>The error I get trying the bulk method is:</p>
  
  <pre><code>*** django.core.exceptions.FieldError: Aggregate functions are not allowed in this query
  </code></pre>
  
  <p>How do I do a seemingly simply count of related records in a bulk update? Ideally I'd like to apply a simple field filter to the count of table B as well.</p>