0

Current models:

class Task(PolymorphicModel):
    pass

class DownloadTask(Task):
    pass

New models: And now i want to do add a new layer called VirtualTask:

class Task(PolymorphicModel):
    pass

class VirtualTask(Task):
    pass

class DownloadTask(VirtualTask):
    pass

Migration it looks like this:

class Migration(migrations.Migration):

    dependencies = [
        ("tasks", "0005_alter_task_last_error"),
    ]

    operations = [
        migrations.CreateModel(
            name="VirtualTask",
            fields=[
                (
                    "task_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="tasks.task",
                    ),
                ),
            ],
            options={
                "abstract": False,
                "base_manager_name": "objects",
            },
            bases=("tasks.task",),
        ),
        migrations.RemoveField(
            model_name="downloadtask",
            name="task_ptr",
        ),
        migrations.AddField(
            model_name="downloadtask",
            name="virtualptask_ptr",
            field=models.OneToOneField(
                auto_created=True,
                default=1,
                on_delete=django.db.models.deletion.CASCADE,
                parent_link=True,
                primary_key=True,
                serialize=False,
                to="tasks.virtualtask",
            ),
            preserve_default=False,
        ),
    ]

“How should I handle existing data when inserting a new intermediate model (VirtualTask) between a base model (Task) and its subclasses in Django

  • should I customize the migration file, and if so, what would be the step-by-step approach?”
3
  • So what does VirtualTask "bring to the table"? Extra fields? How will you populate these? Commented May 20 at 9:18
  • Yes it will bring some extra fields. i just tried to show the relevant part. and the question is more on how to keep the existing Task objects. Commented May 20 at 9:47
  • but how will you populate/transfer the fields from DownloadTask to the VirtualTask in the middle? Commented May 20 at 9:51

0

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.