So I have the given model:
class FooBar(models.Model):
foo = models.ForeignKey(Foo,null=True,blank=True)
bar = models.ForeignKey(Bar,null=True,blank=True)
foo_flag = models.BooleanField(default=False)
bar_flag = models.BooleanField(default=False)
where the logic was that at all times, there can either be a foreign key to Foo or Bar and not both.But now the logic has changed so that there is always a Foo foreign key and sometimes a Bar. So my new model looks as such:
class FooBar(models.Model):
foo = models.ForeignKey(Foo)
bar = models.ForeignKey(Bar,null=True,blank=True)
bar_flag = models.BooleanField(default=False)
Now here is the complex part. The Bar model looks as such:
class Bar(models.Model):
foo = models.ForeignKey(Foo)
so for every previously existing item in the database where the foo field is null and therefore there is a foreign key to Bar, I need the foo field to get a foreign key to the same Foo object that has the bar fields object has a foreign key to. Here is the logic stepped out:
- delete
FooBar.foo_flag - populate all null
fooforeign keys with theFooobjects from theBarforeign keys - no longer allow null in
foofield
How could I go about writing this migration?