picador
picador

Reputation: 155

use Django database models externally

My site is named ficosa. I have a Django server set up with a sqlite database that contains a table named core_Data. Now, I am developing a python file named serverMQTT.py that should insert data into that sqlite database. This file is outside Django so in order to import the Django models from ficosa site I call django.setup()

import django
from django.conf import settings
from ficosa import settings as fsettings

settings.configure(default_settings=fsettings, DEBUG=True)

django.setup()

Now this script or any imported module can use any part of Django it needs.

from core.models import Data

However, I am having an error:

AttributeError: 'module' object has no attribute 'LOGGING_CONFIG'

I would be gratefull if sombody could help me

Upvotes: 1

Views: 454

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599550

That's not how you configure settings in a standalone app. Note what the documentation says:

Be aware that if you do pass in a new default module, it entirely replaces the Django defaults, so you must specify a value for every possible setting that might be used in that code you are importing.

Presumably, your fsettings module only contains database settings. In which case, just override that one thing:

settings.configure(DATABASES=fsettings.DATABASES, DEBUG=True)

Upvotes: 3

Related Questions