How to switch the direction of a Django OneToOneField?

旧街凉风 提交于 2019-12-30 06:48:52

问题


I currently have a couple of models set up like this:

from django.db import models

class Child(models.Model):
    child_name = models.CharField(max_length=200)

class Parent(models.Model):
    parent_name = models.CharField(max_length=200)
    child = models.OneToOneField(Child, null=True, blank=True)

Unfortunately this is wrong, because I want a delete on the parent to cascade to the child and not vice-versa, so I really should have them set up like this:

class Parent(models.Model):
    parent_name = models.CharField(max_length=200)

class Child(models.Model):
    child_name = models.CharField(max_length=200)
    child = models.OneToOneField(Parent)

Assuming that I don't currently have any orphan children, how can I set up the django migration such that my data remains intact? I'm using a Postgres database, if that makes any difference.


回答1:


First, add a OneToOneField to the child model, while keeping the original field.

class Parent(models.Model):
    parent_name = models.CharField(max_length=200)
    child = models.OneToOneField('Child', null=True, blank=True, related_name='+')

class Child(models.Model):
    child_name = models.CharField(max_length=200)
    parent = models.OneToOneField(Parent, null=True, blank=True, related_name='+')

I've set the related_name='+' so that the reverse relationships do not clash with the other field. You might find the data migration is easier if you specify a related name for one or both of the fields.

Create a migration for the new field.

Then create a data migration to populate the new field.

Finally, remove the old field, and create a new migration.



来源:https://stackoverflow.com/questions/32996758/how-to-switch-the-direction-of-a-django-onetoonefield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!