Code

Opened 5 years ago

Last modified 13 months ago

#11264 new Bug

"from django.db import models" clobbered in functions inside __init__.py of installed apps

Reported by: akaihola Owned by: nobody
Component: Database layer (models, ORM) Version: master
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 ramiro)

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_APPS
  • myfunc is in myapp/__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.

Attachments (1)

11264_models_clobber_test_project.zip (2.6 KB) - added by akaihola 5 years ago.
a minimal test project to reproduce "models" clobbering

Download all attachments as: .zip

Change History (9)

Changed 5 years ago by akaihola

a minimal test project to reproduce "models" clobbering

comment:1 in reply to: ↑ description Changed 5 years ago by akaihola

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 5 years ago by akaihola

The issue this causes in Satchmo was reported in a thread on the Satchmo users Google Group.

comment:3 Changed 4 years ago by russellm

  • Triage Stage changed from Unreviewed to 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 Changed 4 years ago by miracle2k

  • Cc miracle2k added

comment:5 Changed 3 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 3 years ago by ramiro

  • Description modified (diff)
  • Easy pickings unset
  • UI/UX unset

comment:7 Changed 13 months ago by aaugustin

This bug still exists in master.

After adjusting settings.py and manage.py for Django 1.6 in the test project, I was able to reproduce it:

>>> import models_clobber_test_app
>>> models_clobber_test_app.assert_models_package()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/myk/Documents/dev/django/models_clobber_test_project/models_clobber_test_app/__init__.py", line 11, in assert_models_package
    'models is %r instead of django.db.models' % models)
AssertionError: models is <module 'models_clobber_test_app.models' from '/Users/myk/Documents/dev/django/models_clobber_test_project/models_clobber_test_app/models.py'> instead of django.db.models

comment:8 Changed 13 months ago by aaugustin

  • Component changed from Core (Other) to Database layer (models, ORM)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.