Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created November 13, 2015 14:40
Show Gist options
  • Save kezabelle/bfc23d1ceb10ef833bbc to your computer and use it in GitHub Desktop.
Save kezabelle/bfc23d1ceb10ef833bbc to your computer and use it in GitHub Desktop.
Moving from abstract to concrete inheritance in Django .... maybe.

step 1

Define a new Model ConcreteThing object pointing a the new base table. Only field is PK. Migrate it into place without changing anything else.

step 2

add a new field on the subclass which should ultimately inherit from ConcreteThing. The field is a OneToOneField to it, which is nullable.

step 3

data migration to create one ConcreteThing for each existing intended subclass instance. while there, having created the ConcreteThing for the subclass to ultimately link to, set the OneToOneField value to the right thing.

step 4

change the primary_key for the subclass to be the OneToOne, and make it not nullable, and set parent_link to True.

step 5

Drop the old pk field, if it still exists.

step 6

Moving other fields will involve the same kind of thing, a 3 step process of adding them to the parent, data migrating the data from the subclass to the parent, then deleting the fields from the subclass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment