Django loaddata设置错误
当尝试在本地计算机(win/sqlite)上使用 loaddata 时:
python django-admin.py loaddata dumpdata.json
我收到以下错误:
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.
我正在使用 djangoconfig 应用程序(如果有帮助):
"""
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了设置初始项目之外,请勿将 django-admin.py 用于任何其他用途。一旦您有了项目,请使用
manage.py
- 它会设置对您的设置文件的引用。Don't use
django-admin.py
for anything other than setting up an initial project. Once you have a project, usemanage.py
instead - it sets up the reference to your settings file.syncdb
将加载 content_types,您需要在加载数据之前清除该表。像这样的事情:另外,请确保在同步数据库时不要创建超级用户或任何用户,因为它们也可能与您的数据装置发生冲突......
syncdb
will load content_types, you need to clear that table before loading data. Something like this: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 ...
有两种标准方法可以向 Django 提供设置。
set DJANGO_SETTINGS_MODULE=mysite.settings
django-admin.py的选项-- settings=mysite.settings
Django-config 的做法有所不同,因为它允许您多个设置文件。 Django-config 与manage.py 一起指定使用哪个。你应该尽可能使用manage.py;它设置了环境。在您的情况下,请尝试以下操作,其中 --settings 指向您要从 django-config 的配置文件夹中使用的特定 .py 文件。
django-admin.py loaddata dumpdata.json --settings=
实际上 --settings 需要 python 包语法,所以可能.config。
你的设置>.py
There are two standard ways to provide your settings to Django.
set DJANGO_SETTINGS_MODULE=mysite.settings
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