Code

Opened 8 years ago

Closed 8 years ago

#1812 closed defect (fixed)

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

Reported by: ian@… Owned by: mtredinnick
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 mattmcc 8 years ago.
Add an ImproperlyConfigured exception when an app has no models module

Download all attachments as: .zip

Change History (10)

Changed 8 years ago by mattmcc

Add an ImproperlyConfigured exception when an app has no models module

comment:1 Changed 8 years ago by mattmcc

  • Summary changed from django-admin sql <foo> gives exception when no model exists to [patch] django-admin sql <foo> gives exception when no model exists

Something like that?

comment:2 Changed 8 years ago by adrian

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

comment:3 Changed 8 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(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 8 years ago by Alex Dedul

  • Resolution fixed deleted
  • Status changed from closed to reopened

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 8 years ago by adrian

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

comment:6 Changed 8 years ago by mtredinnick

  • Owner changed from adrian to mtredinnick
  • Status changed from reopened to new

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 8 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 8 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 8 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


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

 
Note: See TracTickets for help on using tickets.