The Django documentation
gives a warning to avoid catching errors inside transaction.atomic() blocks, and to use nested transactions if you need to do so. But in the case where we have code like:
with transaction.atomic():
# Create and save some models here
try:
SomeModel.objects.get(id=NON_EXISTENT_ID)
except SomeModel.DoesNotExist:
raise SomeCustomError()
Will anything bad actually happen if we just immediately raise a custom error without doing any other error handling? The expected behavior in this case would be that the entire transaction gets rolled back, and so nothing before or after the exception is committed.
I'm just wondering in cases like these there is any reason for using the recommended nested transaction, or if it's just extra code that's not serving any purpose. The examples only speak to cases where there is there would otherwise be database queries getting executed in between the first database error and the end of the transaction, which isn't the case here.