Django loaddata设置错误

发布于 2024-11-03 06:26:02 字数 1461 浏览 5 评论 0原文

当尝试在本地计算机(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃温暖了心ぐ 2024-11-10 06:26:02

除了设置初始项目之外,请勿将 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, use manage.py instead - it sets up the reference to your settings file.

给我一枪 2024-11-10 06:26:02

syncdb 将加载 content_types,您需要在加载数据之前清除该表。像这样的事情:

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

另外,请确保在同步数据库时不要创建超级用户或任何用户,因为它们也可能与您的数据装置发生冲突......

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 ...

离去的眼神 2024-11-10 06:26:02

有两种标准方法可以向 Django 提供设置。

  • 使用set(或在Unix上导出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.

  • 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文