diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2013-09-25 16:10:43 +0100 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2013-09-25 16:10:43 +0100 |
| commit | fe9f342d8ceae96a03295e56ec743ed2e2e5fb51 (patch) | |
| tree | f8d62ecaab2c5302a6f6a256d6cafbfc87848879 /django | |
| parent | 8a3e543f268ba027a631274d1cfe44db64ad9025 (diff) | |
Allow callables as the argument to RunPython
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/migrations/operations/special.py | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/django/db/migrations/operations/special.py b/django/db/migrations/operations/special.py index 89b13c46a0..6f7dec8c74 100644 --- a/django/db/migrations/operations/special.py +++ b/django/db/migrations/operations/special.py @@ -1,6 +1,7 @@ import re import textwrap from .base import Operation +from django.utils import six class SeparateDatabaseAndState(Operation): @@ -107,12 +108,17 @@ class RunPython(Operation): reversible = False def __init__(self, code): - # Trim any leading whitespace that is at the start of all code lines - # so users can nicely indent code in migration files - code = textwrap.dedent(code) - # Run the code through a parser first to make sure it's at least - # syntactically correct - self.code = compile(code, "<string>", "exec") + if isinstance(code, six.string_types): + # Trim any leading whitespace that is at the start of all code lines + # so users can nicely indent code in migration files + code = textwrap.dedent(code) + # Run the code through a parser first to make sure it's at least + # syntactically correct + self.code = compile(code, "<string>", "exec") + self.is_callable = False + else: + self.code = code + self.is_callable = True def state_forwards(self, app_label, state): # RunPython objects have no state effect. To add some, combine this @@ -124,11 +130,14 @@ class RunPython(Operation): # object, representing the versioned models as an AppCache. # We could try to override the global cache, but then people will still # use direct imports, so we go with a documentation approach instead. - context = { - "models": from_state.render(), - "schema_editor": schema_editor, - } - eval(self.code, context) + if self.is_callable: + self.code(models=from_state.render(), schema_editor=schema_editor) + else: + context = { + "models": from_state.render(), + "schema_editor": schema_editor, + } + eval(self.code, context) def database_backwards(self, app_label, schema_editor, from_state, to_state): raise NotImplementedError("You cannot reverse this operation") |
