I saw that from 1.7 there is https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.ready, however this is per each app.
What I am looking for is a way to call some initialization code when Django is loaded, regardless of which apps it contains.
Many people suggest adding the code to wsgi.py, urls.py and settings.py, however all these methods are 'dirty'.
My scenario is that I have several apps that could be part of the Django deployment, and I would like any subset to have this init code.
I could write a special common app, but I assume there must be a Django way of providing init code.
In newer versions of Django, you generally have a project directory containing the global, app-independent files like settings.py and wsgi.py. You can add global initialization code to __init__.py in that directory.
Be advised that this code is run very early (even before the settings are loaded), so some parts of the Django API are not available yet.
Related
I want to create a web app with Django and integration of Selenium and Python-Docker API.
Where inside the Django project is the correct place to put the Selenium scripts so users will be able the run it by a puse of a button?
I've seen some thred saying inside a 'view', but is is a lot of logic that I've sprated into multipale .py files.
You must import Selenium package inside another file such as sel-functions.py inside your app.
Then you define your class or functions inside this file. Finally you can import all those functionality to your views.py file.
This is best and cleanest way to work with another package or functionality in Django project.
Also if multiple apps of your project need to sel-functions.py you can put that in your project directory beside settings.py.
Also, if your code has grown so much that it has multiple files, you
need to define a package for your intended work and define it as an
application in your project.
After Django's 8 part tutorial there is an additional "Advanced Tutorial" which gives a quick-start like approach into python packaging. Namely, moving the Django tutorial app "polls" outside of a Django project and preparing it to be pushed to PyPi.
While this information is useful and covers a bit more ground that Python's own packaging tutorial (e.g. the MANIFEST file to include static / template resources). It doesn't quite explain how to make an app reusable in the sense of configurable options.
For example, if the app has a runtime dependency variable my_bool, how one would open up setting this option for their reusable app.
My first instinct was AppConfig. However, subclassing AppConfig for this purpose isn't explained in the documentation.
One could assume that all the options / settable variables come from the main settings.py file and set defaults if not there... but that also isn't explicitly stated or recommended in any of the Django docs that I have read thus far.
For a reusable application I am making, I would like the user to be able to augment the app with their own data files.
Thus they would need to supply these files in a directory somewhere (with a specified, designated structure) and then tell my app where they are e.g. MY_APP_DATA_DIR="os.path.join(BASE_DIR, <location>)".
Where would such variables go in a reusable Django app?
Would one use a dependency like python-decouple to grab these values?
Most documentation simply tells you to add the name of each of your apps to the INSTALLED_APPS array in your Django project's settings. What is the benefit/purpose of this? What different functionality will I get if I create 2 apps, but only include the name of one in my INSTALLED_APPS array?
Django uses INSTALLED_APPS as a list of all of the places to look for models, management commands, tests, and other utilities.
If you made two apps (say myapp and myuninstalledapp), but only one was listed in INSTALLED_APPS, you'd notice the following behavior:
The models contained in myuninstalledapp/models.py would never trigger migration changes (or generate initial migrations). You wouldn't be able to interact with them on the database level either because their tables will have never been created.
Static files listed within myapp/static/ would be discovered as part of collectstatic or the test server's staticfiles serving, but myuninstalledapp/static files wouldn't be.
Tests within myapp/tests.py would run but myuninstalledapp/tests.py wouldn't.
Management commands listed in myuninstalledapp/management/commands/ wouldn't be discovered.
So really, you're welcome to have folders within your Django project that aren't installed apps (you can even create them with python manage.py startapp) but just know that certain auto-discovery Django utilities won't work for that application.
I've done some monkeypatching to some third-party apps in my custom Django 1.10 app, and now, when I try to create an initial migration, it also generates migrations for these external apps, but it puts those migrations in my virtualenv's site-packages directory, where they can't be version controlled.
Moreover, the monkeypatching doesn't actually change the schema. I'm just changing the verbose name and help text to be more user-friendly. There are no changes being made to the database, so there's no actual need to generate migrations for them. How do I generate my app's migration without generating them for external apps?
I tried deleting them, and removing them from my app's migration's dependencies list, but then my unittests won't run and I get the error:
django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for...
I have core django admin project that when my customers purchase needs to be modified both in configuration and function. Each customer will have their own instance of the project installed on a different server. Currently I am using django apps to separate out the difference in clients and using settings.py to load the correct app for the correct customer.
So my questions:
Is there a industry standard/best practice/framework to customize configuration and functionality in django admin projects and distribute them?
What would be best to do is write your core project on one side, and store it in a repository (version control). Apart from that, write your sub-applications as requirements and store/track them on separate version control.
Then, to keep everything working together and keep it all integrated, what you should do is that for different functionality, write it all in the same app, but add special settings that change the app's behavior. These settings can be stored in an independent settings_local.py that is imported at the end of your settings file, making them installation-independent, and you keep in your settings.py those that are general to all installations.