Skip to content

Instantly share code, notes, and snippets.

@qoelet
Created October 18, 2011 16:48
Show Gist options
  • Save qoelet/1295911 to your computer and use it in GitHub Desktop.
Save qoelet/1295911 to your computer and use it in GitHub Desktop.
South's freeze_apps
def freeze_apps(apps):
"""
Takes a list of app labels, and returns a string of their frozen form.
"""
if isinstance(apps, basestring): # @kenny: checks that the passed in arg is str/unicode
apps = [apps] # if 'apps' is a str/unicode, convert to list
frozen_models = set()
# For each app, add in all its models
for app in apps:
for model in models.get_models(models.get_app(app)):
# @kenny: where the magic happens, see blog post further down :)
# Only add if it's not abstract or proxy
if not model._meta.abstract and not getattr(model._meta, "proxy", False):
frozen_models.add(model)
# Now, add all the dependencies
for model in list(frozen_models):
frozen_models.update(model_dependencies(model))
# Serialise!
model_defs = {} # @kenny: the hot sauce - dictionary of dictionaries containing dictionary of the field names as key, and a tuple (fields info)
model_classes = {}
for model in frozen_models:
model_defs[model_key(model)] = prep_for_freeze(model)
model_classes[model_key(model)] = model
# Check for any custom fields that failed to freeze.
missing_fields = False
for key, fields in model_defs.items():
for field_name, value in fields.items():
if value is None:
missing_fields = True
model_class = model_classes[key]
field_class = model_class._meta.get_field_by_name(field_name)[0]
print " ! Cannot freeze field '%s.%s'" % (key, field_name)
print " ! (this field has class %s.%s)" % (field_class.__class__.__module__, field_class.__class__.__name__)
if missing_fields:
print ""
print " ! South cannot introspect some fields; this is probably because they are custom"
print " ! fields. If they worked in 0.6 or below, this is because we have removed the"
print " ! models parser (it often broke things)."
print " ! To fix this, read http://south.aeracode.org/wiki/MyFieldsDontWork"
sys.exit(1)
return model_defs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment