Opened 15 years ago
Last modified 10 years ago
#11264 closed Bug
"from django.db import models" clobbered in functions inside __init__.py of installed apps — at Version 6
Reported by: | Antti Kaihola | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | importing |
Cc: | miracle2k | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
If an app myapp
has the following __init__.py
:
from django.db import models def myfunc(): assert models.__package__ == 'django.db.models', ( 'models is %r instead of django.db.models' % models)
then calling myapp.myfunc()
throws:
AssertionError: models is <module 'myapp.models' from 'myapp/models.pyc'> instead of django.db.models
This happens only if
myapp
is in INSTALLED_APPSmyfunc
is inmyapp/__init__.py
, not some other module in it- Django is r10088 or later (thanks, git bisect)
This breaks at least Satchmo from a few months back for me, probably trunk too.
r10088 claims to have fixed all dynamic imports in Django by backporting importlib
from Python 2.7.
Change History (7)
by , 15 years ago
Attachment: | 11264_models_clobber_test_project.zip added |
---|
comment:1 by , 15 years ago
Replying to akaihola:
Messed up the assertion error -- for myapp
it's of course
AssertionError: models is <module 'myapp.models' from 'myapp/models.pyc'> instead of django.db.models
So what happens is that inside the function models
points to the app's own models.py
instead of django.db.models
.
comment:2 by , 15 years ago
The issue this causes in Satchmo was reported in a thread on the Satchmo users Google Group.
comment:3 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
Some extra debug: If your test case invokes
import myapp myapp.myfunc()
or
import myapp from myapp import model myapp.myfunc()
everything works as expected. It's only when you run
import myapp from myapp.models import * myapp.myfunc()
that you get a problem.
comment:4 by , 15 years ago
Cc: | added |
---|
comment:5 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:6 by , 13 years ago
Description: | modified (diff) |
---|---|
Easy pickings: | unset |
UI/UX: | unset |
a minimal test project to reproduce "models" clobbering