Django Tips

REPL driven development

In addition to working with Django admin for testing, you can also get REPL access to your Django environment:

$ python manage.py shell

and do things like test out how to access or filter a model, etc

Same with the db env:

$ python manage.py dbshell

Migrations

See migations with showmigrations:

$ python manage.py showmigrations

Or create a migation:

$ python manage.py makemigration

Migrate with migrate, or roll back a migration by referencing the migration before it for the given section:

$ pythom manage.py migrate core 002_migation_to_rollback_to

A migration that involves additional processing such as a backfill can also be done. Create an empty migration and then use RunPython:

from django.db import migrations

def run_backfill(apps, schema_editor):
    MyModel = apps.get_model('core', 'MyModel')
    db_alias = schema_editor.connection.alias
    model = MyModel.objects.using(db_alias).get(...)
    ...

class Migration(migations.Migration):
    dependencies = [
        ('core', '003_backfill')
    ]

    operations = [
        migrations.RunPython(run_backfill)
    ]

Performance

Serving production static files