Skip to content

Instantly share code, notes, and snippets.

@joaodubas
Created March 30, 2012 21:57
Show Gist options
  • Save joaodubas/2255788 to your computer and use it in GitHub Desktop.
Save joaodubas/2255788 to your computer and use it in GitHub Desktop.
Example of data migration for add/remove multi-table inheritance
class Migration(DataMigration):
def forwards(self, orm):
"""Based on solution:
http://stackoverflow.com/questions/4064808/django-model-inheritance-create-sub-instance-of-existing-instance-downcast
"""
parent_articles = orm['app.Parent'].objects.all()
for article in parent_articles:
children_article = orm['app.Children(parent_ptr=article)
children_article.__dict__.update(article.__dict__)
children_article.save()
def backwards(self, orm):
"""Based on solution:
http://stackoverflow.com/questions/3711191/django-deleting-object-keeping-parent
"""
children_articles = orm['app.Children'].objects.all()
attrs = {
'title': 'Primeiro texto',
}
for article in children_articles:
dummy = orm['app.Parent'].objects.create(**attrs)
article.parent_ptr = dummy
article.save()
article.delete()
class Parent(models.Model):
title = models.CharField(max_length=128)
class Children(Parent):
subtitle = models.CharField(max_length=128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment