Opened 10 years ago

Closed 10 years ago

#1812 closed defect (fixed)

[patch] django-admin sql <foo> gives exception when no model exists

Reported by: ian@… Owned by: Malcolm Tredinnick
Component: Core (Management commands) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

hi.
I have a application which has no models in it.
when I run
django-admin.py sql text
it gives me the following exception.

Traceback (most recent call last):
  File "/usr/local/bin/django-admin.py", line 5, in ?
    management.execute_from_command_line()
  File "/magik/django/core/management.py", line 1195, in execute_from_command_line
    mod_list = [models.get_app(app_label) for app_label in args[1:]]
  File "/magik/django/db/models/loading.py", line 29, in get_app
    return __import__(app_name, '', '', ['models']).models
AttributeError: 'module' object has no attribute 'models'

it should fail nicely.

Attachments (1)

loading-getapp.diff (804 bytes) - added by Matt McClanahan 10 years ago.
Add an ImproperlyConfigured exception when an app has no models module

Download all attachments as: .zip

Change History (10)

Changed 10 years ago by Matt McClanahan

Attachment: loading-getapp.diff added

Add an ImproperlyConfigured exception when an app has no models module

comment:1 Changed 10 years ago by Matt McClanahan

Summary: django-admin sql <foo> gives exception when no model exists[patch] django-admin sql <foo> gives exception when no model exists

Something like that?

comment:2 Changed 10 years ago by Adrian Holovaty

That's probably not the right place to put the error...

comment:3 Changed 10 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

(In [3201]) Fixes #1812 -- Added model validity checks to ensure that models.py exists, and has been successfully imported for all INSTALLED_APPS. Previous behaviour was to silently ignore empty/problem models, which resulted in the display of an admin page that doesn't display a supposedly installed model. Thanks to Ian Holsman for the original report.

comment:4 Changed 10 years ago by Alex Dedul

Resolution: fixed
Status: closedreopened

There are cases when no models exist in an application and with r3201 its now fails on syncdb with such apps installed. Trivial fix to this is to bring back removed AttributeError exception handler:

--- django/db/models/loading.py (revision 3206)
+++ django/db/models/loading.py (working copy)
@@ -27,6 +27,8 @@
         for app_name in settings.INSTALLED_APPS:
             try:
                 load_app(app_name)
+            except AttributeError:
+                pass # This app doesn't have a models.py in it.
             except Exception, e:
                 # Problem importing the app
                 _app_errors[app_name] = e

comment:5 Changed 10 years ago by Adrian Holovaty

I don't believe that change is in the right place -- i.e., it will have side effects.

comment:6 Changed 10 years ago by Malcolm Tredinnick

Owner: changed from Adrian Holovaty to Malcolm Tredinnick
Status: reopenednew

I'm neck deep in that part of the code at the moment trying to sort out a few problems. I'll take care of this one. The proposed patch traps a few real errors, so isn't right. We can fix it at the source fairly easily.

comment:7 Changed 10 years ago by remco@…

This also affects people that have the zope page template exceptionformatter installed from http://www.zope.org/Members/shh/DjangoPageTemplates

comment:8 Changed 10 years ago by remco@…

Work around for the zope page templates exceptionformatter is of course to add an empty models.py to the exceptionformatter directory in django/contrib/

I have situations where multiple apps share models and some apps do not have their own model, these now all contain an empty models.py. But is this really required? Why can it not be possible to have apps without a models.py?

comment:9 Changed 10 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [3221]) Fixed #1812 -- permit apps without models (without disguising other errors).

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