I delay the DatabaseError exception and check if there is a ContentType entry for the model that I am trying to use if I do not assume that syncdb is occurring, otherwise the transaction will be rolled back and the original exception raised again. This method takes on additional access to the database only when a DatabaseError is created.
with transaction.commit_on_success():
try:
content_type = ContentType.objects.get_for_model(kwargs['instance'])
for relation in WorkflowTypeRelation.objects.filter(content_type=content_type):
workflow_instance = WorkflowInstance.objects.create(content_object=kwargs['instance'],
workflow_type=relation.workflow_type)
except DatabaseError as database_error:
try:
ContentType.objects.get(model='workflowtyperelation')
except ContentType.DoesNotExist:
pass
except DatabaseError:
raise database_error
else:
transaction.rollback()
raise database_error
source
share