Programmatically check if syncdb is running

I have signal handlers that work with a Django user. In addition, I use South. These signal handlers depend on some migrations that must be performed earlier.

When Django starts snycdb and creates the admin user, these migrations fail and the signal handlers throw an exception.

I am looking for a way to determine if Django starts syncdb so that signal handlers can skip execution.

+5
source share
3 answers

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:
                # 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
+2
source

I don't think there is a way to find out if syncdb works, but there is a way to handle exceptions in python:

Just catch and exception and pass:

try:
    # code that deals with django user
except ExceptionYouAreGetting:
    # seems like syncdb is running, let pass for now
    pass
0
source

AFAIK , syncdb, syncdb loaddata raw.

@receiver(post_save, sender=ModelA)
def signal_handler(sender, **kwargs):
    raw = kwargs.get('raw', False)
    if not raw:
        <do whatever>

, , syncdb.

0

All Articles