Opened 11 years ago
Closed 11 years ago
#22057 closed Bug (fixed)
Importing `reverse_lazy` in settings.py leads to ImportError
Reported by: | Akis Kesoglou | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | dev |
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)
Change History (14)
comment:1 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 11 years ago
Cannot reproduce even though there is a clear circular import. Does anybody have a self-contained app showing the problem?
by , 11 years ago
Attachment: | test22057bare.zip added |
---|
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
by , 11 years ago
Attachment: | patch_commit_29974eaec3ec.patch added |
---|
comment:4 by , 11 years ago
Added a quick patch that fixes the issue. Run tests with sqlite, all pass.
comment:5 by , 11 years ago
I think it's better to just move the !DjangoJSONEncoder into the JSONResponse
by , 11 years ago
Attachment: | patch_commit_d27adb83dd26.patch added |
---|
comment:6 by , 11 years ago
Added another patch, fixing the issue by lazily importing DjangoJSONEncoder
inside JSONResponse.__init__
.
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.
comment:7 by , 11 years ago
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 by , 11 years ago
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.
by , 11 years ago
Attachment: | 22057.diff added |
---|
Removed import time instanciations in django/db/models/sql/aggregates.py
comment:9 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Marking as accepted as discussed with apollo13