#29738: Django can't serialize DateTimeTZRange(lower=None, upper=None, 
bounds='[)')
-------------------------------------+-------------------------------------
     Reporter:  Graham Mayer         |                    Owner:  Can
                                     |  Sarıgöl
         Type:  Bug                  |                   Status:  closed
    Component:  Migrations           |                  Version:  2.0
     Severity:  Normal               |               Resolution:  fixed
     Keywords:  rangefield           |             Triage Stage:  Accepted
  postgresql psycopg2 migrations     |
  removed                            |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Jon Dufresne):

 After running a bisect, I've found that this change caused a regression
 for my project. This occurs when running management commands. It looks
 like `from django.db.migrations.writer import MigrationWriter` is not safe
 at the module level when `django.setup()` is called. Here is the stack
 trace:

 {{{
 Traceback (most recent call last):
   File ".../myproject/manage.py", line 12, in <module>
     execute_from_command_line(sys.argv)
   File ".../django/django/core/management/__init__.py", line 381, in
 execute_from_command_line
     utility.execute()
   File ".../django/django/core/management/__init__.py", line 357, in
 execute
     django.setup()
   File ".../django/django/__init__.py", line 24, in setup
     apps.populate(settings.INSTALLED_APPS)
   File ".../django/django/apps/registry.py", line 91, in populate
     app_config = AppConfig.create(entry)
   File ".../django/django/apps/config.py", line 116, in create
     mod = import_module(mod_path)
   File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in
 import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
   File "<frozen importlib._bootstrap>", line 967, in
 _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 728, in exec_module
   File "<frozen importlib._bootstrap>", line 219, in
 _call_with_frames_removed
   File ".../django/django/contrib/postgres/apps.py", line 8, in <module>
     from django.db.migrations.writer import MigrationWriter
   File ".../django/django/db/migrations/writer.py", line 10, in <module>
     from django.db.migrations.loader import MigrationLoader
   File ".../django/django/db/migrations/loader.py", line 8, in <module>
     from django.db.migrations.recorder import MigrationRecorder
   File ".../django/django/db/migrations/recorder.py", line 9, in <module>
     class MigrationRecorder:
   File ".../django/django/db/migrations/recorder.py", line 22, in
 MigrationRecorder
     class Migration(models.Model):
   File ".../django/django/db/models/base.py", line 99, in __new__
     app_config = apps.get_containing_app_config(module)
   File ".../django/django/apps/registry.py", line 252, in
 get_containing_app_config
     self.check_apps_ready()
   File ".../django/django/apps/registry.py", line 135, in check_apps_ready
     raise AppRegistryNotReady("Apps aren't loaded yet.")
 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
 }}}

 This change appears to fix it for me, but I haven't had a chance to fully
 verify its correctness or write a test.

 {{{
 diff --git a/django/contrib/postgres/apps.py
 b/django/contrib/postgres/apps.py
 index 97475de6f7..151450f4e4 100644
 --- a/django/contrib/postgres/apps.py
 +++ b/django/contrib/postgres/apps.py
 @@ -5,7 +5,6 @@ from psycopg2.extras import (
  from django.apps import AppConfig
  from django.db import connections
  from django.db.backends.signals import connection_created
 -from django.db.migrations.writer import MigrationWriter
  from django.db.models import CharField, TextField
  from django.test.signals import setting_changed
  from django.utils.translation import gettext_lazy as _
 @@ -22,6 +21,7 @@ def uninstall_if_needed(setting, value, enter,
 **kwargs):
      Undo the effects of PostgresConfig.ready() when
 django.contrib.postgres
      is "uninstalled" by override_settings().
      """
 +    from django.db.migrations.writer import MigrationWriter
      if not enter and setting == 'INSTALLED_APPS' and
 'django.contrib.postgres' not in set(value):
          connection_created.disconnect(register_type_handlers)
          CharField._unregister_lookup(Unaccent)
 @@ -42,6 +42,7 @@ class PostgresConfig(AppConfig):
      verbose_name = _('PostgreSQL extensions')

      def ready(self):
 +        from django.db.migrations.writer import MigrationWriter
          setting_changed.connect(uninstall_if_needed)
          # Connections may already exist before we are called.
          for conn in connections.all():
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29738#comment:14>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.584a5ee8c1e846e63e8afc83dc826113%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to