0

I' trying to migrate this model:

class Questionpart_image(models.Model):
    questionpart = models.ForeignKey(Questionpart, null=True, blank=True)
    image = models.ImageField()

to this:

class Questionpart_image(Questionpart): # notice this base class
    image = models.ImageField()

to make advantage of inheritance. Django produces the following migration:

class Migration(migrations.Migration):

dependencies = [
    ('ochsite', '0016_auto_20150809_1903'),
]

operations = [
    migrations.RemoveField(
        model_name='questionpart_image',
        name='id',
    ),
    migrations.RemoveField(
        model_name='questionpart_image',
        name='questionpart',
    ),
    migrations.AddField(
        model_name='questionpart_image',
        name='questionpart_ptr',
        field=models.OneToOneField(default='', primary_key=True, to='ochsite.Questionpart', serialize=False, parent_link=True, auto_created=True),
        preserve_default=False,
    ),
]

but this does not set the right foreign key to questionpart_ptr from questionpart field. How can I achieve that?
I've been searching for a long time, but nothing...thanks

1 Answer 1

1

Simply don't rely on automatic migrations, make your own or modify that one.

Simplest solution will be by moving AddField in migration to the top of the list and inject between it and RemoveFields RunPython block that will rewrite id's from old field to new (and in other direction in reverse, if needed).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I ended up with creating completely new models, migrating data and deleting old models. It's a bit more complicated but it's safe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.