DJANGO_SETTINGS_MODULE ERROR using Django models from python shell

7 views
Skip to first unread message

Bruno Tikami

unread,
May 11, 2007, 8:41:52 AM5/11/07
to django...@googlegroups.com
Hi!

I'm trying to use Django models from a python script and I'm having some settings problem. Even though I called settings.configure, it keeps ordering for some other setting parameter. The error is listed below. Any idea?



$ python
>>> import recorder
>>> import recorder.settings
>>> from django.conf import settings
>>> settings.configure(recorder.settings, DEBUG=True)
>>> import recorder.six_core
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "recorder/six_core/__init__.py", line 1, in ?
    from recorder.six_core.models       import Server_Status
  File "/usr/six/recorder/six_core/models.py", line 1, in ?
    from django.db import models
  File "/usr/lib/python2.4/site-packages/django/db/__init__.py", line 30, in ?
    connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)
  File "/usr/lib/python2.4/site-packages/django/conf/__init__.py", line 32, in __getattr__
    return getattr(self._target, name)
  File "/usr/lib/python2.4/site-packages/django/conf/__init__.py", line 133, in __getattr__
    return getattr(self.default_settings, name)
AttributeError: 'module' object has no attribute 'DATABASE_OPTIONS'



Tks,


Tkm

Malcolm Tredinnick

unread,
May 11, 2007, 8:53:06 AM5/11/07
to django...@googlegroups.com
On Fri, 2007-05-11 at 09:41 -0300, Bruno Tikami wrote:
> Hi!
>
> I'm trying to use Django models from a python script and I'm having
> some settings problem. Even though I called settings.configure, it
> keeps ordering for some other setting parameter. The error is listed
> below. Any idea?
>
>
>
> $ python
> >>> import recorder
> >>> import recorder.settings
> >>> from django.conf import settings
> >>> settings.configure(recorder.settings, DEBUG=True)

The first argument here is the global defaults dictionary. Normally,
Django will take your settings file and merge it with
django.conf.global_settings, however, if you are using manual
configuration, you need to make sure that every single default setting
is given a value. This is documented in the manual configuration
section, although we may need to put it in big red flashing lights,
because it gets overlooked.

Manual configuration isn't really designed to make it trivial to specify
your normal settings file and some overrides like you are trying to do
here. One way to do this, though, is to read through your settings file
yourself and put all the options into a dictionary (say settings_dict),
then call

settings.configure(**dict(settings_dict, DEBUG=True))

Alternatively, create a file that combines your settings with
global_settings and then use that as the replacement global (the first
positional arg in the settings.configure() call).

Regards,
Malcolm


Malcolm Tredinnick

unread,
May 11, 2007, 8:58:23 AM5/11/07
to django...@googlegroups.com
On Fri, 2007-05-11 at 22:53 +1000, Malcolm Tredinnick wrote:
[...]

> Manual configuration isn't really designed to make it trivial to specify
> your normal settings file and some overrides like you are trying to do
> here.

I should point out that there wasn't really any technical reason for
this decision and it's not because we're mean people. We just didn't
think that there might be a lot of use-cases when you wouldn't want to
specify all the changes from default yourself. Running scripts, however,
is the obvious one we forgot about.

So if you (or anybody else) wants to come up with a backward-compatible
change that allows for specifying a module that is laid over the top of
global_settings and then have the keyword args applied, go for it. Open
a ticket with the patch and I will give it serious consideration.

Regards,
Malcolm


Bruno Tikami

unread,
May 11, 2007, 10:11:44 AM5/11/07
to django...@googlegroups.com
Hello Malcolm

Thanks for you fast reply!!!!

I'll try to put the missing settings on my project.settings . Do I have to call configure() in some diferent way after I do that? I mean, if my settings have all the global settings...

Malcolm Tredinnick

unread,
May 11, 2007, 10:19:27 AM5/11/07
to django...@googlegroups.com
Hi Bruno,

On Fri, 2007-05-11 at 11:11 -0300, Bruno Tikami wrote:
> Hello Malcolm
>
> Thanks for you fast reply!!!!
>
> I'll try to put the missing settings on my project.settings . Do I
> have to call configure() in some diferent way after I do that? I mean,
> if my settings have all the global settings...

No. The first argument to configure() is a class or a module, so that
the code can access each of the settings as an attribute. So if you
project settings contains every single global setting, you will be able
to pass that in (boy is that going to be a big file, though). Note that
you may not actually need every single global setting, but you will need
a fair chunk of them, I expect.

Regards,
Malcolm


Bruno Tikami

unread,
May 11, 2007, 10:21:03 AM5/11/07
to django...@googlegroups.com
Malcolm, it works!!!!


Thanks a lot, I shall delete the unecessary global settings.

TKS!!!


Tkm

On 5/11/07, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:

Bruno Tikami

unread,
Aug 7, 2007, 2:01:04 PM8/7/07
to django...@googlegroups.com
Hi everybody (specially Malcom),

After a long period, I had time to delte the unecessary variables and test it. So, finally, the goes the list of settigns you must include on you default settings file in order to use you application models outside Django web context (a python server side script for instance).

gettext_noop = lambda s: s
DATABASE_OPTIONS = {}
URL_VALIDATOR_USER_AGENT = "Django/0.96pre ( http://www.djangoproject.com)"

It works on my actual project.

Good luck!

Tkm
Reply all
Reply to author
Forward
0 new messages