[Python] Use Django ORM outside of Django [duplicate]

Discussão em 'Python' iniciado por Stack, Março 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'm new to Django and want to use its ORM in my scripts without running whole Django thing. I'm scratching my head how to configure it. Searches on StackOverflow didn't help as answers don't show the full picture.

    Therefore, I created a small project:

    app.py
    manage.py
    orm/
    __init__.py
    models.py


    manage.py has configuration:

    from django.conf import settings
    settings.configure(
    DATABASE_ENGINE = 'mysql',
    DATABASE_NAME = 'db',
    DATABASE_USER = 'admin',
    DATABASE_PASSWORD = '',
    DATABASE_HOST = 'localhost',
    INSTALLED_APPS = ('orm')
    )


    models.py:

    from django.db import models
    class Label(models.Model):
    name = models.CharField(max_length=50) # required max_length


    and finally my main file app.py:

    from django.conf import settings
    from django.db import models
    from orm.models import *
    \# do database maniupaltions


    Though after running app.py I receive an error that: django.core.exceptions.ImproperlyConfigured: 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.

    What am I doing wrong?

    Continue reading...

Compartilhe esta Página