# Buggy code
def handle(self, *args, **options):
    hours = options["age"]

    if hours:
        age = timezone.now() - timedelta(hours=hours)

    for translation in self.get_translations(**options):
        if not translation.repo_needs_commit():
            continue

        if not hours:
            age = timezone.now() - timedelta(
                hours=translation.subproject.commit_pending_age
            )

        last_change = translation.last_change
        if last_change is None:
            continue
        if last_change > age:
            continue

        if int(options["verbosity"]) >= 1:
            self.stdout.write("Committing {0}".format(translation))
        translation.commit_pending(None)


# Traceback (django.db.transaction.TransactionManagementError)
Traceback (most recent call last):
File "./manage.py", line 31, in <module>
execute_from_command_line(sys.argv)
File "/home/www-data/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/home/www-data/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/www-data/.local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/nijel/weblate_demo/weblate/trans/management/commands/__init__.py", line 42, in execute
super(WeblateCommand, self).execute(*args, **options)
File "/home/www-data/.local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/nijel/weblate_demo/weblate/trans/management/commands/commit_pending.py", line 68, in handle
translation.commit_pending(None)
File "/home/nijel/weblate_demo/weblate/trans/models/translation.py", line 582, in commit_pending
request, last, self.last_change, True, True, skip_push
File "/home/nijel/weblate_demo/weblate/trans/models/translation.py", line 692, in git_commit
self.update_units(author)
File "/home/nijel/weblate_demo/weblate/trans/models/translation.py", line 721, in update_units
for unit in self.unit_set.filter(pending=True).select_for_update():
File "/home/www-data/.local/lib/python2.7/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/home/www-data/.local/lib/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/www-data/.local/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/home/www-data/.local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 876, in execute_sql
sql, params = self.as_sql()
File "/home/www-data/.local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 468, in as_sql
raise TransactionManagementError('select_for_update cannot be used outside of a transaction.')
django.db.transaction.TransactionManagementError: select_for_update cannot be used outside of a transaction.

# Fixed code
def handle(self, *args, **options):
    hours = options["age"]

    if hours:
        age = timezone.now() - timedelta(hours=hours)

    for translation in self.get_translations(**options):
        if not translation.repo_needs_commit():
            continue

        if not hours:
            age = timezone.now() - timedelta(
                hours=translation.subproject.commit_pending_age
            )

        last_change = translation.last_change
        if last_change is None:
            continue
        if last_change > age:
            continue

        if int(options["verbosity"]) >= 1:
            self.stdout.write("Committing {0}".format(translation))
        with transaction.atomic():
            translation.commit_pending(None)