F
F
fantom_ask2020-07-26 08:05:10
Django
fantom_ask, 2020-07-26 08:05:10

How to edit a database from another external file?

I want to add a couple of values ​​to my database from a separate file
I tried to include models.py but I got an error

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.


I know that django has a python manage.py shell command but it only works from the console.
Is it possible to do the same but only from a separate file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-07-26
@sergey-gornostaev

Your best bet is to write a management command . Or to fence similar crutches in your script:

import django
from django.conf import settings

settings.configure(
    DATABASES={
        'default': {
            'ENGINE': '<your_engine>',
            'NAME': '<database_name>',
            'HOST': '<hostname_or_ip>',
            'PORT': '<port>',
            'USER': '<user>',
            'PASSWORD': '<super_secret_password>',   
        }
    },
    INSTALLED_APPS=[
        '<your_app>',
    ]
)
django.setup()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question