def emit(self, record):
try:<EOL><INDENT>from .models import Model, Application, ModelObject<EOL>from django.contrib.contenttypes.models import ContentType<EOL>if not hasattr(record, '<STR_LIT:action>'):<EOL><INDENT>from .settings import AUTOMATED_LOGGING<EOL>signal = True<EOL>for excluded in AUTOMATED_LOGGING['<STR_LIT>']:<EOL><INDENT>if record.module.startswith(excluded):<EOL><INDENT>signal = False<EOL>break<EOL><DEDENT><DEDENT>if '<STR_LIT>' not in AUTOMATED_LOGGING['<STR_LIT>']:<EOL><INDENT>signal = False<EOL><DEDENT>if signal:<EOL><INDENT>from .models import Unspecified<EOL>entry = Unspecified()<EOL>if hasattr(record, '<STR_LIT:message>'):<EOL><INDENT>entry.message = record.message<EOL><DEDENT>entry.level = record.levelno<EOL>entry.file = record.pathname<EOL>entry.line = record.lineno<EOL>entry.save()<EOL>if self.maxage:<EOL><INDENT>current = timezone.now()<EOL>Unspecified.objects.filter(created_at__lte=current - self.maxage).delete()<EOL><DEDENT><DEDENT>return<EOL><DEDENT>if record.action == '<STR_LIT>':<EOL><INDENT>if ('<STR_LIT>' in record.data['<STR_LIT>'].__dict__.keys() and<EOL>record.data['<STR_LIT>'].al_evt):<EOL><INDENT>entry = record.data['<STR_LIT>'].al_evt<EOL><DEDENT>else:<EOL><INDENT>entry = Model()<EOL>entry.user = record.data['<STR_LIT:user>']<EOL>entry.save()<EOL>name = record.data['<STR_LIT>']._meta.app_label<EOL>entry.application = Application.objects.get_or_create(name=name)[<NUM_LIT:0>]<EOL>if hasattr(record, '<STR_LIT:message>'):<EOL><INDENT>entry.message = record.message<EOL><DEDENT>if record.data['<STR_LIT:status>'] == '<STR_LIT>':<EOL><INDENT>status = <NUM_LIT:1><EOL><DEDENT>elif record.data['<STR_LIT:status>'] == '<STR_LIT>':<EOL><INDENT>status = <NUM_LIT:2><EOL><DEDENT>elif record.data['<STR_LIT:status>'] == '<STR_LIT>':<EOL><INDENT>status = <NUM_LIT:3><EOL><DEDENT>else:<EOL><INDENT>status = <NUM_LIT:0><EOL>from automated_logging.settings import AUTOMATED_LOGGING<EOL>if not AUTOMATED_LOGGING['<STR_LIT>']:<EOL><INDENT>entry.delete()<EOL>return None<EOL><DEDENT><DEDENT>entry.action = status<EOL>entry.information = ModelObject()<EOL>entry.information.value = repr(record.data['<STR_LIT>'])[:<NUM_LIT:255>]<EOL>ct = ContentType.objects.get_for_model(record.data['<STR_LIT>'])<EOL>try:<EOL><INDENT>ContentType.objects.get(pk=ct.pk)<EOL>ct_exists = True<EOL><DEDENT>except ContentType.DoesNotExist:<EOL><INDENT>ct_exists = False<EOL><DEDENT>if ct_exists:<EOL><INDENT>entry.information.type = ct<EOL><DEDENT>entry.information.save()<EOL>record.data['<STR_LIT>'].al_evt = entry<EOL><DEDENT>if ('<STR_LIT>' in record.data['<STR_LIT>'].__dict__.keys() and<EOL>record.data['<STR_LIT>'].al_chl and<EOL>not entry.modification):<EOL><INDENT>entry.modification = record.data['<STR_LIT>'].al_chl<EOL><DEDENT>entry.save()<EOL>if self.maxage:<EOL><INDENT>current = timezone.now()<EOL>Model.objects.filter(created_at__lte=current - self.maxage).delete()<EOL><DEDENT><DEDENT>elif record.action == '<STR_LIT>':<EOL><INDENT>from .models import Request<EOL>if record.data['<STR_LIT>'] is not None:<EOL><INDENT>entry = Request()<EOL>entry.application = record.data['<STR_LIT>']<EOL>entry.uri = record.data['<STR_LIT>'][:<NUM_LIT:255>]<EOL>entry.user = record.data['<STR_LIT:user>']<EOL>entry.status = record.data['<STR_LIT:status>']<EOL>entry.method = record.data['<STR_LIT>']<EOL>entry.save()<EOL><DEDENT>if self.maxage:<EOL><INDENT>current = timezone.now()<EOL>Request.objects.filter(created_at__lte=current - self.maxage).delete()<EOL><DEDENT><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>print(e)<EOL><DEDENT>
Handler instance that records to the database.

try and except is preventing a circular import.
Reference:
http://stackoverflow.com/questions/4379042/django-circular-model-import-issue