This is an old question, but the answer saved me a lot of efforts. I just want to add something.
When making a model class to inherite from an abstract model, Django migrations will remove that model.
MyModel(models.Model):
# some fields to be inherited later from the abstract model
author = models.ForeignKey('auth.User')
# other fields specific to this model
Now if you create an abstract model:
MyAbstractModel(models.Model):
# fields to be used by children classes
class Meta:
abstract = True
and let your model inherit from it:
MyModel(MyAbstractModel):
author = models.ForeignKey('auth.User')
# other fields specific to this model
If you run makemigrations and migrate on your app Django will remove that model and delete the correspondent DB table.
You can outsmart Django by commenting out the code for that model. Django will remove it with all other consequences. Now you can uncomment the code and run the migrations again and Django will create the DB table again.
However you will likely have your admin, views, forms, etc., to import your model. This means the migrations will throw error, because that class can't be found. You'll have to comment out the code in all files where your model is imported.
Easier would be to skip the commenting out and write the wanted migration manually:
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
# app_label should be your app
# and 000x_the_last_migration is the name of the migration
# it depends on, usually the last one
('app_label', '000x_the_last_migration'),
]
operations = [
# do this for all ForeignKey, ManyToManyField, OneToOneField
# where model_name is obviously the model name and name is the
# field name
migrations.RemoveField(
model_name='mymodel',
name='author',
),
# finally delete the model
migrations.DeleteModel(
name='MyModel',
),
]
Now you can run the migration:
python manage.py migrate app_label
later inherit from the abstract model (see code above, 3rd block) and make new migration:
python manage.py makemigrations app_label
This should save you commenting out large chunks of code. If you want to save the data in the DB table, then you can use dumpdata
and loaddata
.
TLDR
This wasn't a step for step guide and requires experience with Django. In short you have to:
- Create abstract model
- Write the migration manually for the model that has to inherit from the abstract model
- Run the migration (this deletes the DB table with all entries!)
- Inherit from the abstract model
- Run makemigrations and migrate