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)

test22057.zip (4.7 KB ) - added by Akis Kesoglou 11 years ago.
test project
test22057bare.zip (3.6 KB ) - added by Akis Kesoglou 11 years 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 Akis Kesoglou 11 years ago.
patch_commit_d27adb83dd26.patch (1.1 KB ) - added by Akis Kesoglou 11 years ago.
22057.diff (1.6 KB ) - added by Claude Paroz 11 years ago.
Removed import time instanciations in django/db/models/sql/aggregates.py

Download all attachments as: .zip

Change History (14)

comment:1 by Akis Kesoglou, 11 years ago

Triage Stage: UnreviewedAccepted

Marking as accepted as discussed with apollo13

comment:2 by Grzegorz Nosek, 11 years ago

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

by Akis Kesoglou, 11 years ago

Attachment: test22057.zip added

test project

comment:3 by Akis Kesoglou, 11 years ago

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

by Akis Kesoglou, 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 Akis Kesoglou, 11 years ago

comment:4 by Akis Kesoglou, 11 years ago

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

comment:5 by anonymous, 11 years ago

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

by Akis Kesoglou, 11 years ago

comment:6 by Akis Kesoglou, 11 years ago

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 11 years ago by Akis Kesoglou (previous) (diff)

comment:7 by Aymeric Augustin, 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 Claude Paroz, 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 Claude Paroz, 11 years ago

Attachment: 22057.diff added

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

comment:9 by Claude Paroz <claude@…>, 11 years ago

Resolution: fixed
Status: newclosed

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