Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#20050 closed Uncategorized (needsinfo)

Django models are not importable for utility scripts

Reported by: anatoly 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?

Change History (3)

comment:1 by Aymeric Augustin, 11 years ago

Resolution: invalid
Status: newclosed

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 by anatoly techtonik, 11 years ago

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 by anatoly techtonik, 11 years ago

Resolution: invalidneedsinfo

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

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