Python script for Django app to access models without using manage.py shell

PythonDjangoDjango Models

Python Problem Overview


I'm writing a script to import some model objects into the database my django application uses. In the past I've solved this by running ./manage.py shell and then import myscript. I'm sure there's a better way. I'd like to be able to call a script from anywhere on my HD using python scriptname.py, and in the first few lines of that script it would do whatever imports / other operations necessary so that it can access model objects and behave as though it was run using manage.py shell.

What do I need to add to my script to achieve this?

EDIT:

Based on @Melug's answer, with addition of dynamically setting Python path to address the 'anywhere on my HD' part of the question:

import sys
sys.path.append('c:\\my_projec_src_folder')
from myproject import settings
from django.core.management import setup_environ
setup_environ(settings)

Python Solutions


Solution 1 - Python

Since Django 1.4 you should avoid using setup_environ(settings) (post by Melug) because it is deprecated. Use the following instead and you will be able to access your model

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings")

# your imports, e.g. Django models
from your_project_name.models import Location

# From now onwards start your script..

Here is an example to access and modify your model:

if __name__ == '__main__':    
    # e.g. add a new location
    l = Location()
    l.name = 'Berlin'
    l.save()
    
    # this is an example to access your model
    locations = Location.objects.all()
    print locations
    
    # e.g. delete the location
    berlin = Location.objects.filter(name='Berlin')
    print berlin
    berlin.delete()

Example model:

class Location(models.Model):
    name = models.CharField(max_length=100)

Solution 2 - Python

To get models loaded too, I had to combine this with this answer, otherwise I get django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
import django
django.setup()

As an extra, I add this to the __init__.py of my django projects, it will automatically discover the app name so it is copy/paste-able:

import os


def setup():
    module = os.path.split(os.path.dirname(__file__))[-1]
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{}.settings".format(module))
    import django
    django.setup()

Then I can just do:

from <app> import setup
setup()

Solution 3 - Python

For Django version 1.9 or later you can use this:

import sys
import os
import django

sys.path.append('your_project_directory')
os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'
django.setup()

from yourapp.models import your_model

so you can use object as same django object:

from myapp.models. import Locations
all_locations = Locations.object.all()
first_location = Locations.object.get(id=1)
print first_location.name()
first_location.save()

Solution 4 - Python

I think the best way is to create your custom management command(s). Then you can call manage.py <yourcommand> from anywhere.

Solution 5 - Python

You need to setup django environment first:

from your_project import settings
from django.core.management import setup_environ
setup_environ(settings)

At last import your models, everything goes just like django.

Solution 6 - Python

FOR DJANGO 1.11

Upper solutions did not work, but gave me an error:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

For me solution from here worked out:

import os
from django.core.wsgi import get_wsgi_application

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()

Solution 7 - Python

Since at least Django 1.11, your main app includes a wsgi module that does the neccessary setup on import. Assuming myproject/myproject is where your settings.py is, in your script just import:

from myproject.wsgi import application

Solution 8 - Python

for django >= 2.0 it is enough to do these 2 imports

from your_project_path import settings as your_project_settings
from django.core.management import settings

then you can do just from my_app.models import MyModel

and work with your Model MyModel

Solution 9 - Python

Here is the answer for Django versions > 1.4:

from django.core.management import settings
from myproject.myproject import settings as project_settings

if not settings.configured:
  settings.configure(default_settings=project_settings)

Solution 10 - Python

If you get:

django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

Try:

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
application = get_wsgi_application()

Solution 11 - Python

Try:

os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"

if os.environ.setdefault doesn't work. (Windows 10, python3.6.4, django 2.0.3)

Solution 12 - Python

As Stavros pointed out here, you can just copy the wsgi.py and put it at the beginning of you script. Besides setting up DJANGO_SETTINGS_MODULE, you also need to get the applications. Hope it helps. It works for me at django 2.1.5.

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'revochem.settings')

application = get_wsgi_application()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTrindazView Question on Stackoverflow
Solution 1 - PythonMichaelView Answer on Stackoverflow
Solution 2 - PythonRebsView Answer on Stackoverflow
Solution 3 - PythonAlkindusView Answer on Stackoverflow
Solution 4 - PythonDrTyrsaView Answer on Stackoverflow
Solution 5 - PythonMelugView Answer on Stackoverflow
Solution 6 - PythonHenhuyView Answer on Stackoverflow
Solution 7 - PythonjdmView Answer on Stackoverflow
Solution 8 - PythonMartin NowosadView Answer on Stackoverflow
Solution 9 - PythonISPView Answer on Stackoverflow
Solution 10 - Pythonjuan IsazaView Answer on Stackoverflow
Solution 11 - PythonyvesvaView Answer on Stackoverflow
Solution 12 - Pythonuser1422543View Answer on Stackoverflow