请求设置DEFAULT_INDEX_TABLESPACE

时间:2017-12-01 09:32:30

标签: python mysql django django-models django-settings

我正在制作一个populate.py脚本,用于将数据从mysql数据库传输到django mysqlite数据库。

运行populate.py脚本时,我一直收到此错误:

Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured.
You must either define the environment variable DJANGO_SETTINGS_MODULE
or call settings.configure() before accessing settings.

所以我从我的研究中发现,我必须添加settings.configure(),所以我这样做但仍然得到错误并且不再了解它可能是什么。

这是我导入的内容:

import django
import time
import datetime
import os
from mysql.connector import connection
from django.conf import settings
from user import models
from techportal import models

settings.configure()
django.setup()
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'itdb.settings')

我已经使用此功能传输数据:

# Insert all technologies
def technologies():
    current = con_mysql.cursor()
    current_query = ("SELECT technology_id title, description, company_id, author, contactor, publish_date, edit_date, is_hidden, visible FROM technologies")
    current.execute(current_query)

    for (technology_id, title, description, company_id, author, contactor, publish_date, edit_date, is_hidden, visible) in current:
        # Converts datetime to (int)unixtimestamp
        if (publish_date == "0000-00-00 00:00:00"):
            publish_date = time.mktime(datetime.datetime.strptime(str(datetime.datetime.now().replace(microsecond=0)),
                                                                   "%Y-%m-%d %H:%M:%S").timetuple())
        else:
            publish_date = time.mktime(datetime.datetime.strptime(publish_date, "%Y-%m-%d %H:%M:%S").timetuple())

        # Converts datetime to (int)unixtimestamp
        if (edit_date == "0000-00-00 00:00:00"):
            edit_date = time.mktime(datetime.datetime.strptime(str(datetime.datetime.now().replace(microsecond=0)),
                                                                   "%Y-%m-%d %H:%M:%S").timetuple())
        else:
            edit_date = time.mktime(datetime.datetime.strptime(edit_date, "%Y-%m-%d %H:%M:%S").timetuple())

        mdl, succeeded = models.PortalTechnology.objects.get_or_create(
            author_id=author,
            basetech_id=technology_id,
            is_hidden=is_hidden,
            visible=visible
        )
        mdl.save()

        mdl, succeeded = models.Technology.objects.get_or_create(
            name=title,
            long_description=description,
            created_on=publish_date,
            supplier_id=company_id
        )
        mdl.save()

    current.close()

1 个答案:

答案 0 :(得分:0)

根据this answerthis answer,您需要先设置env变量,然后调用django.setup()

文件 your_script.py

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
import django
django.setup()

# here goes the rest of your script ...
import time
import datetime
from mysql.connector import connection
from user import models
from techportal import models

def technologies():
    ...

自Django 1.7起,这是必需的(请参见release notes)。

这种方式不必使用settings.configure()