Search code examples
pythonmysqldjangodjango-modelsdjango-settings

Requested setting DEFAULT_INDEX_TABLESPACE


I'm making a populate.py script to transfer data from a mysql database to a django mysqlite database.

I keep getting this error when I run my populate.py script:

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.

So what I found from my researches was that I had to add settings.configure(), so I did that but still getting the error and have no clue anymore what it could be.

This is what I import:

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')

I've made this function to transfer the data:

# 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()

Solution

  • According to this answer and also this answer you need to first set the env variable and then call django.setup():

    File 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():
        ...
    

    This is necessary since django 1.7 (see release notes).

    And settings.configure() should not be necessary this way.