From 6ce7887f1387855df2cda180da08277febedd02e Mon Sep 17 00:00:00 2001 From: Nasir Hussain Date: Wed, 23 Jan 2019 03:49:30 +0500 Subject: [2.2.x] Fixed #30111 -- Fixed AppRegistryNotReady error with django.contrib.postgres in INSTALLED_APPS. Regression in e192223ed996ed30fe83787efdfa7f2be6b1a2ee. Backport of 2804b8d2153505ec49b191db2168302dfb92c3af from master. --- django/db/migrations/recorder.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'django') diff --git a/django/db/migrations/recorder.py b/django/db/migrations/recorder.py index 3a972fe4c6..ad5435d906 100644 --- a/django/db/migrations/recorder.py +++ b/django/db/migrations/recorder.py @@ -1,6 +1,7 @@ from django.apps.registry import Apps from django.db import models from django.db.utils import DatabaseError +from django.utils.decorators import classproperty from django.utils.timezone import now from .exceptions import MigrationSchemaMissing @@ -18,19 +19,30 @@ class MigrationRecorder: If a migration is unapplied its row is removed from the table. Having a row in the table always means a migration is applied. """ - - class Migration(models.Model): - app = models.CharField(max_length=255) - name = models.CharField(max_length=255) - applied = models.DateTimeField(default=now) - - class Meta: - apps = Apps() - app_label = "migrations" - db_table = "django_migrations" - - def __str__(self): - return "Migration %s for %s" % (self.name, self.app) + _migration_class = None + + @classproperty + def Migration(cls): + """ + Lazy load to avoid AppRegistryNotReady if installed apps import + MigrationRecorder. + """ + if cls._migration_class is None: + class Migration(models.Model): + app = models.CharField(max_length=255) + name = models.CharField(max_length=255) + applied = models.DateTimeField(default=now) + + class Meta: + apps = Apps() + app_label = 'migrations' + db_table = 'django_migrations' + + def __str__(self): + return 'Migration %s for %s' % (self.name, self.app) + + cls._migration_class = Migration + return cls._migration_class def __init__(self, connection): self.connection = connection -- cgit v1.3