0

_Hi, everyone! I want to change from ForeignKey to Many-to-Many, but I afraid to break my database. This is my class which I want to change:

class Ninja(models.Model):
id_user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="ninja", blank=True, null=True)
id_team = models.ForeignKey("mission.Team", null=True, blank=True, on_delete=models.SET_NULL)

With this piece of code each user can entry only one team. But I want each user could be in different teams. So, I think I need many-to-many. Hope, I'm right.

The question is: 'If I change my model field like this:

id_team = models.ManyToManyField("mission.Team", null=True, blank=True, on_delete=models.SET_NULL)

will I broke my database with actual data? I have a very negative experience with droping database twice in past. :(

Sergo
  • 93
  • 3
  • 10
  • Hey, thank you, but I don't understand in the answer after "And add the following content to it. To understand exactly how this works, check the documentation on Data Migrations. Be careful not to overwrite the migration dependency." Where would the function be? In what part of code... :] – Sergo Jul 14 '22 at 20:45
  • 1
    That code is put into the empty migration file, it will need some editing to suit your models and migration dependencies – 0sVoid Jul 14 '22 at 20:48

0 Answers0