Created
March 30, 2012 21:57
-
-
Save joaodubas/2255788 to your computer and use it in GitHub Desktop.
Example of data migration for add/remove multi-table inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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