I have some signal handlers that work on the Django user. Additionally I'm using South. These signal handlers depends on some migrations that must run in before.
When Django executed snycdb and creates the admin user these migrations haven't run and the signal handlers raise an exception.
I'm searching for a way to detect if Django currently runs syncdb so that the signal handerls can skip execution.
I trap the DatabaseError exception and check if the ContentType entry for the model I'm trying to use exists, if it doesn't I assume syncdb is happening, otherwise rollback the transaction and re-raise the original exception. This method incurs in an extra DB access only when DatabaseError is raised.
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:
# Most probable running during syncdb phase,
# so ignore the exception
pass
except DatabaseError:
# ContentType model DB table doesn't exists,
# raise original exception
raise database_error
else:
# The ContentType model exists,
# there is something wrong with the DB
# raise original exception
transaction.rollback()
raise database_error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With