Code

Opened 16 months ago

Closed 16 months ago

Last modified 16 months ago

#20050 closed Uncategorized (needsinfo)

Django models are not importable for utility scripts

Reported by: techtonik Owned by: nobody
Component: Core (Other) Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

application/models.py often use settings.py, and because Django can not import its own settings, an attempt to import models from ordinary command line helper script fails.

My application layout:

+ codereview/
|  + __init__.py
|  + models.py
|
+ settings.py
+ data_schema.py

data_schema.py

from codereview import models

And the error:

  ...
  File "codereview\models.py", line 676, in Account
    default_context = db.IntegerProperty(default=settings.DEFAULT_CONTEXT,
  File "...\django-1.4.5-py2.7.egg\django\utils\functional.py", line 184, in inner
    self._setup()
  File "...\django-1.4.5-py2.7.egg\django\conf\__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

I know about os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' workaround. The questions are:

  • Why users need to specify it every time explicitly?
  • Why 'settings' is not the sane default?
  • Why this error can not be shown only after Django actually fails to find the settings?

Attachments (0)

Change History (3)

comment:1 Changed 16 months ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

This looks like a usage question rather than a bug report :)

Since Django 1.4, the canonical value for DJANGO_SETTINGS_MODULE is <myapp>.settings and not settings. settings works for you because you're using a non-standard layout, but it isn't a sane default in general.

Please review the discussions on django-developers about the new project layout implemented in 1.4 for more information.

If you'd like to provide an alternative, please write to the django-developers mailing-list. This is the appropriate medium to discuss new features or refactorings.

comment:2 Changed 16 months ago by techtonik

Can you share these links? Because this answer doesn't explain anything. =)

For a feature request, why not to display sane user message directing where to find this DJANGO_SETTINGS_MODULE?
Failure to specify config is not an application level error, so no need to throw up with cryptic stacktraces.

comment:3 Changed 16 months ago by techtonik

  • Resolution changed from invalid to needsinfo

I also don't understand what is the problem to check package root directory for settings if settings inside package are not found?

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.