Skip to content

Django development

Scott Ouellette edited this page May 2, 2017 · 22 revisions
  1. Database migrations
  2. Context Processors
  3. Generate Model Dependency Diagram

Database migrations

Database migrations are handle by South. Take a look at South's documentation for detailed information.

To avoid migration conflicts (Teams and Workflow):

  • talk to the team before creating a new migration
  • push migrations ASAP
  • don’t edit migrations by hand to resolve conflicts (roll back and reapply)

Procedure:

  1. Make changes to Django model(s)
  2. Create a new migration: ./manage.py schemamigration <app_name> --auto
  3. Apply the migration: ./manage.py syncdb --migrate or ./manage.py migrate <app_name>

Note: that the ./manage.py schemamigration command has an --update mode that allows you to further iteratively refine your migration as such changes become necessary while working on your code. E.g. ./manage.py schemamigration <app_name> --auto --update will update the most recent migration instead of creating a new one.

Troubleshooting:

  • The Admin interface still shows the old schema.

    Make sure to restart the server. E.g. in dev mode run $ supervisorctl restart runserver.

  • Error in Migration

    Try reverting to an earlier migration. ./manage.py migrate {your_app_name} {migration_number}

    For example:

     ./manage.py migrate analysis_manager 0001
    

Context Processors

A context processor has a very simple interface: It’s just a Python function that takes one argument, an HttpRequest object, and returns a dictionary that gets added to the template context. This allows us to expose specific Django settings throughout our templates and javascript.

Adding settings to the template context: To add a specific Django setting to the template context, simply add that settings to the dict returned by core.context_processors.extra_context()

Accessing these values: In Django templates: {{ name_of_setting }}

In JavaScript: name_of_setting

Generating Django Model Diagram:

PyCharm includes a nifty feature that allows you to generate diagrams of your Django models and their dependencies.

Instructions here: https://www.jetbrains.com/help/pycharm/2016.1/viewing-model-dependency-diagram.html

Model diagram generated from commit: https://github.com/parklab/refinery-platform/commit/c107ba15029d30215e3a379448329a91a8911b1c

model_diagram_5-24-16

Adding new FileTypes/FileExtensions to Refinery:

Sometimes we run into FileTypes & FileExtensions that Refinery doesn't know about. To combat this, we can add new types & extensions using a data migration.

See here for a good example: https://github.com/refinery-platform/refinery-platform/blob/develop/refinery/file_store/migrations/0002_filetypes_and_fileextensions.py

Clone this wiki locally