Django and Oscar

Django-admin and

Published 2021-02-12. Last modified 2021-03-28.
Time to read: 2 minutes.

This page is part of the django collection.


django-admin can be run for a specific Django webapp as To set this up, right after running django-admin startproject to create a new Django webapp, make in the new directory executable:

$ chmod a+x

Notes on selected django-admin / subcommands follow. I discuss the dumpdata and test subcommands in separate articles. Additional sections that rely on subcommands conclude this article.

dbshell Subcommand

The dbshell subcommand of the command picks up the database parameters from Django settings for the webapp and passes them to the PostgreSQL psql command. A psql subshell can be spawned:

(aw) $ ./ dbshell
psql (12.5 (Ubuntu 12.5-0ubuntu0.20.10.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.


A single SQL statement can be executed:

(aw) $ ./ dbshell -- -tc 'select current_user'

diffsettings Subcommand

The diffsettings subcommand displays differences between the current settings file and Django’s default settings.

(aw) $ ./ diffsettings | less

loaddata Subcommand

Round-tripping the data involves first dumping the data from the database using the dumpdata subcommand, and then loading it from the dump file using the loaddata subcommand, like this:

(aw) $ ./ loaddata frobshop/fixtures/all.json
Installed 706 object(s) from 1 fixture(s) 

The Django docs suggest that loading data from a fixture deletes pre-existing data in the affected tables. I wonder what the corner cases for this look like, for example a fixture that affects just a property of an app?

Each time you run loaddata, the data will be read from the fixture and re-loaded into the database. Note this means that if you change one of the rows created by a fixture and then run loaddata again, you’ll wipe out any changes you’ve made.

To test my understanding of this, I created an empty fixture:

(aw) $ echo '{}' > frobshop/fixtures/empty.json

Now I loaded the empty fixture:

(aw) $ ./ loaddata frobshop/fixtures/empty.json
/var/work/django/oscar/lib/python3.8/site-packages/django/core/management/commands/ RuntimeWarning: No fixture data found for 'empty'. (File format may be invalid.)
Installed 0 object(s) from 1 fixture(s) 

(aw) $ ./ dumpdata
Lots and lots of JSON is output 

That did not go as expected. The documentation should say that affected records in the database will be overwritten. That is different from my understanding of the current phrasing, which suggests to me that the entire contents of affected tables are replaced.

runserver Subcommand

Run Django for development purposes on all IP addresses and port 8001:

(aw) $ ./ runserver 8001

By default, the development server doesn’t serve static files. To change this, read Managing static files.

shell Subcommand

Individual Python expressions or entire programs can be executed from the Django environment by using the shell subcommand.

(aw) $ ./ shell -h
usage: shell [-h] [--no-startup] [-i {ipython,bpython,python}] [-c COMMAND] [--version] [-v {0,1,2,3}]
                       [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color]

Runs a Python interactive interpreter. Tries to use IPython or bpython, if one of them is available. Any standard
input is executed as code.

optional arguments:
  -h, --help            show this help message and exit
  --no-startup          When using plain Python, ignore the PYTHONSTARTUP environment variable and ~/
  -i {ipython,bpython,python}, --interface {ipython,bpython,python}
                        Specify an interactive interpreter interface. Available options: "ipython", "bpython", and
  -c COMMAND, --command COMMAND
                        Instead of opening an interactive shell, run a command as Django and exit.
  --version             show program’s version number and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g. "myproject.settings.main". If this isn’t provided,
                        the DJANGO_SETTINGS_MODULE environment variable will be used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions
  --no-color            Don’t colorize the command output.
  --force-color         Force colorization of the command output.

A program can be piped in via STDIN:

(aw) $ cat > << EOF
import django

(aw) $ cat | ./ shell

* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.