Skip to content

Instantly share code, notes, and snippets.

@yakky
Last active September 15, 2023 11:12
Show Gist options
  • Save yakky/31466588da712e25e1f2c200d56d3776 to your computer and use it in GitHub Desktop.
Save yakky/31466588da712e25e1f2c200d56d3776 to your computer and use it in GitHub Desktop.
Migrate a plugin from one type to another
from django.db import migrations
def migrate_to_new_plugin(apps, schema_editor):
OldPluginModel = apps.get_model("myapp", "PluginModel")
NewPluginModel = apps.get_model("myapp", "PluginModel")
for obj in OldPluginModel.objects.all():
#
new_obj = NewPluginModel()
new_obj.id = obj.id
new_obj.placeholder = obj.placeholder
new_obj.parent = obj.parent
new_obj.position = obj.position
new_obj.language = obj.language
new_obj.creation_date = obj.creation_date
new_obj.depth = obj.depth
new_obj.path = obj.path
new_obj.numchild = obj.numchild
new_obj.plugin_type = "MyNewPluginName"
# Add something like `new_obj.field_name = obj.field_name` for any field in the the new plugin
obj.delete()
new_obj.save()
# Copy any many to many field after save:`new_plugin.many2many.set(old_plugin.many2many.all())`
class Migration(migrations.Migration):
dependencies = [
("myapp", "other_migration"),
]
operations = [migrations.RunPython(migrate_to_new_plugin, migrations.RunPython.noop)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment