Django model access from custom file - python

Can I receive access to Model from some custom file. For example I create folder in my project with name Bot. Create some custom_file.py, in current file call model from other app.
For example:
from trading.models import Values
Then I get 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.
Also try solution like this in my custom_file.py:
import os
import django
os.environ["DJANGO_SETTINGS_MODULE"] = 'trading.settings'
django.setup()
But still doesn't work.

You don't need to do any fancy stuff from within the file, just create it in your app module (folder) and import any model you want, exactly as in the example you provided.

Related

How do I import Django models into python file?

I have a Django model which stores inputs from a form, now that I have the data stored in Django I need to import those models to a python file.
As per documentation,
If you’re using components of Django “standalone” – for example,
writing a Python script which loads some Django templates and renders
them, or uses the ORM to fetch some data – there’s one more step
you’ll need in addition to configuring settings.
After you’ve either set DJANGO_SETTINGS_MODULE or called configure(),
you’ll need to call django.setup() to load your settings and populate
Django’s application registry. For example:
import django
from django.conf import settings
from myapp import myapp_defaults
settings.configure(default_settings=myapp_defaults, DEBUG=True)
django.setup()
# Now this script or any imported module can use any part of Django it needs.
from myapp import models

Override settings.py for testing

I have my project settings in settings.py file. There are database names, user names, host names etc. defined there. In project files I do import settings and then use the constants where needed like settings.HOST. For unit testing I would like to use different settings. How should I override the settings? I am not using django.
You could create a new file - say local_settings.py in which you override the specific settings for you debugging and testing purposes.
Then you add this block at the end of your settings.py
# Override these settings with local settings if such a file exists
try:
from local_settings import *
except ImportError as e:
pass
You should add local_settings.py to your .gitignore file to exclude this file from version control (if you are using git).
This is the standard way Django does this by the way.
I suppose the easiest way would be to move your settings.py to another folder for safekeeping and then make a new one, and edit that for debugging.

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

How would you go about creating a "config" class in Django?

I'm making a very very reusable CMS in Django. I'm trying not to hardcode anything, so I want a config.py or something for the app itself.
I want to use it in templates (something like {{ config.SITE_NAME }}) and just regular Python code (views, models), etc.
How would I go about doing that?
Django already has the settings.py file and an interface to use the values from there, is that not good enough? Accessing settings in your code is easy:
from django.conf import settings
do_something_with(settings.MY_CONFIG_VARIABLE)
In your template it requires a bit more work, like making a context processor for example. This answer shows you have to make a context processor that exposes values from settings to your template.
settings.py serves this purpose already in django. You can add custom settings to it for your application.
https://docs.djangoproject.com/en/dev/topics/settings/#using-settings-in-python-code
When you create a django project, it automatically creates a settings.py file. Put your settings in that file, and then :-
from django.conf import settings
settings.YOUR_SETTING
also, if you have some settings, that will vary depending upon the machines (eg. production/test servers).
Then just add eg. conf/local_settings.py file and put the machine specific settings in that file, and in settings.py just do :-
from conf.local_settings import *
at the end of settings.py
Make sure you ignore local_settings.py from your VCS check-in (eg. in case of git add local_settings.py to .gitignore

Django: get the path to the current django app

I have a Django app with some management commands. In one of those commands, I need to write a log file which is located in /logs allong side views.py, models.py, etc ...
Considering that my management command script is in /management/commands/mycommand.py, is there any reliable way to determine the location of the appdir from within mycommand.py ?
PS: by reliable, I mean without using os.path.abspath( __file__ ), because the location of the file might change later on.
That's what your settings are for.
Add this to your settings
MY_APP_LOG_DIRECTORY = "path/to/logs"
In your view functions, use
from django.conf import settings
Now you can use settings.MY_APP_LOG_DIRECTORY all you want.

Categories