def recache_n_events_in_locations(place_ids, all=False): """ Recache the number of events for the given locations (by ID). :param all: recache all places instead :type place_ids: Iterable[str] """ # needed so we don't empty the blasted iterator mid-operation place_ids = tuple(set(place_ids)) with transaction.atomic(): if all: Place.objects.update(n_events=0) else: # set the flag to false here, so zero-event places will get it too Place.objects.filter(id__in=place_ids).update(n_events=0, n_events_changed=False) for place_id, n_events in count_events_for_places(place_ids, all=all).items(): Place.objects.filter(id=place_id).update(n_events=n_events)