Django Development and Staging

I have two boxes: dev and production. I need a very simple method for working on the dev machine and then pushing to production.


Use direnv to maintain environment based on directory.

Use two seperate .envrc files on dev and production. Don’t check-in to version control.

Testing and Syncing Databases

Create a test case:

# phisaverweb/

class TestEmails(TestCase):
    fixtures = ['phisaverweb/fixtures/email-tests.json']

    def test_send(self):
        print ("hi")

Tests use a seperate database. To get data to test against, it’s best to programmatically create it as part of test setup.

But to play with the production database, you want to grab it’s current data. First, dump the current database. If you get django test fixtures contenttypes.ContentType unique constraint duplicate key type error, I don’t understand why but “natural primary” and “natural foreign” (see below) fixes it. Also some background.

# original server
python dumpdata --indent 4 --natural-primary --natural-foreign > dump.json
# new server
scp .
# psql> create database phisaver if required migrate # create database schema 
manage loaddata dump.json

Then run the server to play locally or run the tests via python tests. In VSCode you can make a launch task:

            "name": "Django Tests",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/",
            "args": [
                "--keepdb", # don't destroy db on exit
                "phisaverweb.tests" # run just these tests
            "django": true,
            "env": {"PYTHONPATH":"${workspaceFolder}/.."}

Leave a Reply

Your email address will not be published. Required fields are marked *