3

When trying to use loaddata on my local machine (win/sqlite):

python django-admin.py loaddata dumpdata.json

I get the following error:

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 am using djangoconfig app if that helps:

"""
Django-config settings loader.
"""

import os

CONFIG_IDENTIFIER = os.getenv("CONFIG_IDENTIFIER")
if CONFIG_IDENTIFIER is None:
    CONFIG_IDENTIFIER = 'local'

# Import defaults
from config.base import *

# Import overrides
overrides = __import__(
    "config." + CONFIG_IDENTIFIER,
    globals(),
    locals(),
    ["config"]
)

for attribute in dir(overrides):
    if attribute.isupper():
        globals()[attribute] = getattr(overrides, attribute)

projects>python manage.py loaddata dumpdata.json --settings=config.base

WARNING: Forced to run environment with LOCAL configuration.

Problem installing fixture 'dumpdata.json': Traceback
 (most recent call last):
  File "loaddata.py", line 174, in handle
    obj.save(using=using)

...


  File "C:\Python26\lib\site-packages\django\db\backends\sqlite3\base.py", line
234, in execute
    return Database.Cursor.execute(self, query, params)
IntegrityError: columns app_label, model are not unique
Eeyore
  • 2,126
  • 7
  • 33
  • 49
  • The `IntegrityError` looks like it's coming from the content types table, one of the contrib packages. I'd bet that the database you're trying to load into already has something in it, probably from a prior failed load. Since it looks like you're using SQLite, jsut delete the database file, `syncdb` if needed to create a fresh database, and then retry your load. – Mike DeSimone Apr 23 '11 at 22:54
  • 1
    Agree with @Mike DeSimone except `syncdb` will initialize content_types with data so you may get the same error (follow his advice to see!). You may have to edit dumpdata.json to take out the data for content_types. – JCotton Apr 23 '11 at 23:40
  • Removed sqlite db. Did syncdb. Tried loaddata = same error. – Eeyore Apr 24 '11 at 01:01

3 Answers3

8

Don't use django-admin.py for anything other than setting up an initial project. Once you have a project, use manage.py instead - it sets up the reference to your settings file.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
3

syncdb will load content_types, you need to clear that table before loading data. Something like this:

c:\> sqlite3 classifier.db
sqlite> delete from django_content_type;
sqlite> ^Z
c:\> python django-admin.py loaddata dumpdata.json

Also, make sure you do not create a superuser, or any user, when you syncdb, as those are likely to also collide with your data fixture ...

Stephan
  • 41,764
  • 65
  • 238
  • 329
1

There are two standard ways to provide your settings to Django.

  • Using set (or export on Unix) set DJANGO_SETTINGS_MODULE=mysite.settings
  • Alternatively as an option with django-admin.py --settings=mysite.settings

Django-config does things differently because it allows you to have multiple settings files. Django-config works with manage.py to specify which to use. You should use manage.py whenever possible; it sets up the environment. In your case try this where --settings points to the specific .py file you want to use from django-config's config folder.

django-admin.py loaddata dumpdata.json --settings=<config/settings.py>

Actually --settings wants python package syntax so maybe <mysite>.config.<your settings>.py

JCotton
  • 11,650
  • 5
  • 53
  • 59
  • I tried it and it didn't work - perhaps because I use djangoconfig app to manage my settings. – Eeyore Apr 23 '11 at 14:53
  • Edited my question with my interpreter's output. manage.py seems to be grabbing the correct config file but now I am getting a new error. – Eeyore Apr 23 '11 at 22:31
  • Actually you shouldn't need --settings with manage.py. Looks like loaddata works with manage.py so try leaving off the settings option. – JCotton Apr 23 '11 at 23:32