I have a Django application in which I want to change a field from a ForeignKey to a ManyToManyField. I want to preserve my old data. What is the simplest/best process to
Probably the best and easiest thing you should do would be:
Create the Many to many field with a different name say
authors = models.ManyToManyField(Author)
write a small function to convert foreignkey values to M2M values:
def convert():
books = Book.objects.all()
for book in books:
if book.author:
li = [book.author.id]
book.authors.append(li)
book.save()
Once it is run, you can delete the author field from the table and run migration again.