#22057 closed Bug (fixed)

Importing `reverse_lazy` in settings.py leads to ImportError

Reported by: dfunckt Owned by: nobody
Component: Uncategorized Version: master
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Create a new project, add an app with a model and import reverse_lazy in settings.py. Running ./manage.py sqlall myapp raises ImportError with the following traceback:

$ ./manage.py sqlall myapp
Traceback (most recent call last):
  File "<project>/settings.py", line 2, in <module>
    from .settings_local import *
  File "<project>/settings_local.py", line 1, in <module>
    from .settings_base import *
  File "<project>/settings_base.py", line 5, in <module>
    from django.core.urlresolvers import reverse_lazy
  File "<virtualenv>/src/django/django/core/urlresolvers.py", line 15, in <module>
    from django.http import Http404
  File "<virtualenv>/src/django/django/http/__init__.py", line 4, in <module>
    from django.http.response import (HttpResponse, StreamingHttpResponse,
  File "<virtualenv>/src/django/django/http/response.py", line 17, in <module>
    from django.core.serializers.json import DjangoJSONEncoder
  File "<virtualenv>/src/django/django/core/serializers/__init__.py", line 23, in <module>
    from django.core.serializers.base import SerializerDoesNotExist
  File "<virtualenv>/src/django/django/core/serializers/base.py", line 6, in <module>
    from django.db import models
ImportError: cannot import name models

Marking as a release blocker as discussed with apollo13 in IRC.

Cause of this regression is commit: https://github.com/django/django/commit/0242134d32aa99a54442211ed05576b7061866d1

Attachments (5)

test22057.zip (4.7 KB) - added by dfunckt 17 months ago.
test project
test22057bare.zip (3.6 KB) - added by dfunckt 17 months ago.
Another test project, just after calling "django-admin startproject". Only edit is the addition of the reverse_lazy import in settings. Simply running "$ ./manage.py" will give an error that only core commands are listed, since project settings can not be imported
patch_commit_29974eaec3ec.patch (1.7 KB) - added by dfunckt 17 months ago.
patch_commit_d27adb83dd26.patch (1.1 KB) - added by dfunckt 17 months ago.
22057.diff (1.6 KB) - added by claudep 17 months ago.
Removed import time instanciations in django/db/models/sql/aggregates.py

Download all attachments as: .zip

Change History (14)

comment:1 Changed 17 months ago by dfunckt

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Marking as accepted as discussed with apollo13

comment:2 Changed 17 months ago by gnosek

Cannot reproduce even though there is a clear circular import. Does anybody have a self-contained app showing the problem?

Changed 17 months ago by dfunckt

test project

comment:3 Changed 17 months ago by dfunckt

I attached a test project. Using Django@master, Python 3.3.

Changed 17 months ago by dfunckt

Another test project, just after calling "django-admin startproject". Only edit is the addition of the reverse_lazy import in settings. Simply running "$ ./manage.py" will give an error that only core commands are listed, since project settings can not be imported

Changed 17 months ago by dfunckt

comment:4 Changed 17 months ago by dfunckt

Added a quick patch that fixes the issue. Run tests with sqlite, all pass.

comment:5 Changed 17 months ago by anonymous

I think it's better to just move the !DjangoJSONEncoder into the JSONResponse

Changed 17 months ago by dfunckt

comment:6 Changed 17 months ago by dfunckt

Added another patch, fixing the issue by lazily importing DjangoJSONEncoder inside JSONResponse.__init__ (as suggested by anonymous above).

I'm not sure how to go about testing for this regression in order to provide a complete patch, so if anyone has any pointers it would help.

Last edited 17 months ago by dfunckt (previous) (diff)

comment:7 Changed 17 months ago by aaugustin

Such patches are fragile. We try to keep imports properly ordered at the top of modules. After a few years it becomes unclear why a specific import doesn't happen at module level, and if it's still needed.

comment:8 Changed 17 months ago by claudep

I think the problem is caused by two lines here: https://github.com/django/django/blob/master/django/db/models/sql/aggregates.py#L14-15

It shouldn't be too difficult to defer these instanciations. I'll attach a patch.

Changed 17 months ago by claudep

Removed import time instanciations in django/db/models/sql/aggregates.py

comment:9 Changed 17 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 45edb9d2359c60952fd791616df887eb95f75746:

Fixed #22057 -- Ensured reverse_lazy can be used in settings

And without causing a circular import. Thanks Akis Kesoglou for
the report.

Note: See TracTickets for help on using tickets.
Back to Top