In Rails I can create a custom a job for rake which does whatever I want to and then run it as "rake my_task". What's a way to do it in Django?
In particular, I need to create such a job which read my own data files in a custom format, processes the data in a special way and them inserts it into the db. And I'm going to run it multiple times, not only once. To achieve that I created a pure python script and ran it but got an error
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.
So a python script didn't work, there must be a django-way to do it.
The "Django Way" would be to create a custom command.
If you want to create an external python script, then there is also a way to do that explained in this stack overflow answer (EDIT: this is actually outdated for django 1.7) along with an example of how to create custom commands. I recommend the django documentation though if you're using the latest version of django.
On a related subject, check out fabric.
Edit:
For a django >= 1.7 standalone script:
import os
import django
from myapp import models
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
django.setup()
print models.MyModel.objects.get(pk=1)
There are 2 ways to do it on top of my head
1 - You can write a custom management command to be invoked by manage.py: https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/
2 - The rake equivalence in python world is pyinvoke: https://github.com/pyinvoke/invoke, which is a successor of fabric that takes some design inspiration from rake itself.
When using invoke, if you need the django environment to be setup, use this https://docs.djangoproject.com/en/dev/ref/applications/#django.setup
Related
I want to have a production ready Django app with Lighsail and for that I'm following two tutorials to achieve this
Deploy Django-based application onto Amazon Lightsail
Deploy A Django Project
From the Bitnami article can see that the AWS documentation follows its Approach B: Self-Contained Bitnami Installations.
According to:
AWS's documentation, my blocker appears in 5. Host the application using Apache, step g.
Bitnami's documentation, where it says
On Linux, you can run the application with mod_wsgi in daemon mode.
Add the following code in
/opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-app.conf:
The blocker relates to the code I'm being asked to add, in particular the final part that has
Alias /tutorial/static "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/contrib/admin/static"
WSGIScriptAlias /tutorial '/opt/bitnami/apps/django/django_projects/tutorial/tutorial/wsgi.py'
More specifically, /home/bitnami/apps/django/. In /home/bitnami/ can only see the following folders
. bitnami_application_password
. bitnami_credentials
. htdocs
. stack
and from them the one that most likely resembles /opt/bitnami/apps/ is /home/bitnami/stack/. Thing is, inside of that particular folder, there's no django folder - at least as far as I can tell (already checked inside some of its folders, like the python one).
The workaround for me at this particular stage is to move to a different approach, Approach A: Bitnami Installations Using System Packages (which I've done and managed to make it work as wrote in this blog post), but I'd like to get it to work using Approach B and hence this question.
The problem here is in the mentioning of the paths for both the project and Django.
In my case, projects are under /home/bitnami/projects/ where I created a Django project named tutorial.
Also, if I run the command
python -c "
import sys
sys.path = sys.path[1:]
import django
print(django.__path__)"
it'll print me the location where Django is installed
['/opt/bitnami/python/lib/python3.8/site-packages/django']
So, the httpd-app.conf should have instead at the end
Alias /tutorial/static "/opt/bitnami/python/lib/python3.8/site-packages/django/contrib/admin/static"
WSGIScriptAlias /tutorial '/home/bitnami/projects/tutorial/tutorial/wsgi.py'
I want to be able to write shared functions that can be accessed in one-off batch scripts and also by the running Django service (to use the ORM)
Currently, I have this in the _init__.py under the my_proj module.
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_proj.blah.blah.settings'
import django
django.setup()
This works fine for one django project. However, now I want to do reference the ORM functions from another django project, "other_proj" in the same repo from an independent script that lives outside both django projects.
Is there a way to "django.setup()" multiple projects at once?
Or, at least, a way to easily toggle the setup between the two projects?
Or is there a better way altogether? (I realize I could create a client library to hit the services while they are running, but would prefer to remove that overhead)
If you want a Django project to access functionality that resides in a different Django project, a client library is an option (as you noted). You could also consider packaging those sets of functionality as re-usable Django apps that you import into each project, or you could abstract them further into re-usable Python modules which get imported into each project. If you're hoping to use the Django ORM from one Project to access data from a different project, then you might be looking for this SO question: How to make two django projects share the same database
I think with more specifics in your question (such as, for example, function X in Project A you wish you could call from Project B) we might be able to be more specific with guidance.
I'm not sure I quite understand the case you're trying to implement here; two things that sound maybe-sort-of like what you're asking for are:
1) Running Django projects under uWSGI in Emperor mode allows you to serve multiple Django projects from one server simultaneously.
2) Django can be configured to run the same project under multiple domains simultaneously using the Sites framework.
I agree, though, that more detail about what you have and what you're trying to accomplish with it is probably necessary to give a satisfying answer.
Inside my Django project, I want to fire up a python shell wherein I can make simple changes to my database (saving new records, updating old ones, etc.) which are then automatically reverted back when I exit the shell. Essentially the same functionality as running:
rails console --sandbox
in a Ruby on Rails project. I'm not looking for all the features of a traditional sandbox such as blocking web-service calls, just the basic database rollback. Is this--or something similar--possible in Django?
I want to use this for testing purposes. I know I can use Django's testing infrastructure to do this, but there are some basic instances where this would be very useful (e.g. quickly confirming that a specific model functions the way I expect it to).
This is mostly done in python manage.py shell (or with django_extensions in INSTALLED_APPS better ... shell_plus) which is python command line from which you can manipulate the data and test some features you don't wan't to program before you test. Rollback in django shell is not possible as I know, but you can have separate settings.py with different DB settings so you will have another database where you can experiment in shell without corrupting your data.
Here some sources:
Django Best Practice: Settings file for multiple environments
Django shell (from DJango docs)
Hello fellow programmers!
I am reading about the functionality of the interactive shell reached through running ...python manage.py shell. This is something that I have done many times before, however I am not quite sure exactly How the actual shell connects with Django. Before asking this question, I have indeed tried to find my way through the official documentation - as well as other posts on this website. This question does not appear to have been presented with an answer a beginner like me can comprehend.
I have read that the manage.py shell-command 'gives us an interactive shell with the Python path set up correctly for Django.'. I know that it can be used to interact with a Django project, and to query configured databases and models - however what happens behind the scenes?
For example, when writing ModelName.objects.all() - what is the origin of .objects and .all(), because it does not seem to be pure python syntax? How does Django understand this?
Is the shell a special python-version provided by Django, with some added syntax that Django understands? Because that would make sense.
Thank you from Sweden!
From what I know, objects comes from the model having a manager.
From the docs:
A Manager is the interface through which database query operations are provided to Django models. At least one Manager exists for every model in a Django application.
Managers exist in django. So when you run the shell, you're just executing python code inside an environment that has django installed. You probably noticed that you have to run from .models import myModel, which means that having models (i.e. django code) isn't inherit to the shell, you need to manually import it.
A shell is just an interactive way of executing code. The only special thing from running it from manage.py is that it will have django and the rest of your packages in its PYTHONPATH.
I'm new to Django. My localhost site is running fine. Since I am using pycharm it is easy to run any file. I decided to run each file in my django project, and came across several errors, such as this one in my views.py:
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.
Even though the site is running, what seems like properly, I'm seeing this message. What is causing this, and is it typical?
You cannot run each file present in your django project individual.
No matter those are file with .py extension. They depend on the django framework to get the project running.
The reason you might be seeing that error is because you might be using the attributes present in the settings.py file which in turn requires django to set the application running like starting the WSGI server, getting all the dependencies and the installed apps ready before you actually use anything.
Understand that Django is a Framework and it relies on many underlying components to actually work. Even thought you can technically run any file in any manner, you cannot start the application itself.
There are other ways to do it if you like to test the application like using django shell by python manage.py shell to check and test the application, which would be a better way of doing individual testing rather than running each file standalone.
You can run any individual python file in a Django Project with Django, but keep in mind that the settings for Django must be supplied. This is not a good practise to run individual file with Django but for debugging purposes you may use it (for example. to test a parser that you wrote with database access),
You have to configure the Django settings before you can do anything with Django on a single file.
from django.conf import settings
settings.configure()
def do_something_with_a_model():
# does something with a model
print "I am done!!"
Note that relative imports may break when running on single files.
(for example. imports like from .another_module import AnotherClass may break when working with single file.)