Sample post-save handler to update the sample's batch count.

    Batches are unpublished by default (to prevent publishing empty batches).
    If the `AUTO_PUBLISH_BATCH` setting is true, the batch will be published
    automatically when at least one published sample is in the batch.

def update_batch_count(instance, **kwargs):
    """Sample post-save handler to update the sample's batch count.

    Batches are unpublished by default (to prevent publishing empty batches).
    If the `AUTO_PUBLISH_BATCH` setting is true, the batch will be published
    automatically when at least one published sample is in the batch.
    """
    batch = instance.batch
    count = batch.samples.filter(published=True).count()

    if count != batch.count:
        batch.count = count
        if AUTO_PUBLISH_BATCH:
            batch.published = bool(count)
        batch.save()