Let’s supposed I created two models:
class Car(models.Model):
name = models.CharField(max_length=50)
size = models.IntegerField()
class Manufacturer(models.Model):
name = models.CharField(max_length=50)
country = models.CharField(max_length=50)
car = models.ManyToManyField(Car)
I added entries to both models, then I realized that each Car was only related to a unique Manufacturer. So, I should convert my ManyToManyField to a ForeignKey:
class Car(models.Model):
name = models.CharField(max_length=50)
size = models.IntegerField()
manufacturer = models.ForeignKey(Manufacturer)
class Manufacturer(models.Model):
name = models.CharField(max_length=50)
country = models.CharField(max_length=50)
How can I do that without losing my entries? I tried to look in South documentation but I did not found this way of conversion…
This is nontrivial, I think you will need three migrations:
ForeignKey.ManyToManytoForeignKey(using theforwardsmethod).ManyToMany.You could possibly merge 1 and 2 or 2 and 3 together, but I wouldn’t recommend it.
Additionally, you should also implement a
backwardsmethod for 2.An example for 2.’s
forwardswould be:Please note that:
backwardsmethod here yet.You will also need to update the code that uses those relationships in step 2. / 3.