I'm working in a Project where there were multiple fields created as M2M using the through related model, however they were wrongly created by then and the related models have no additional fields, so I would like to use a regular M2M managed by Django.
The existing models I have are:
cass Student(models.Model):
# ... other fields..."
courses = models.ManyToManyField(
Course,
related_name='students',
through='StudentCourse'
)
class StudentCourse(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
cass Course(models.Model):
# ... other fields..."
And I would like to have:
class Student(models.Model):
# ... other fields..."
course = models.ManyToManyField(Course, related_name='students')
class Course(models.Model):
# ... other fields..."
I'm not able to find a way in Django to do this without losing the data that was already inserted.
I was thinking of renaming the tables in the same way Django does and manipulate the Django migration metadata but it is an ugly way to solve it.
Is there a Django way to solve this without losing the data (or creating backup tables)?