[Answered ]-How to change a char field to list field or array field in Django model?

1👍

First you need to create a model for Device like so:

class Device(models.Model)
    name = models.CharField(max_length=100)

Then you can add a new field to User model (Do not remove old one yet).

device_ids = models.ManyToManyField(YouDeviceModel)

migrate database

py manage.py makemigrations
py manage.py migrate

Then add current files inside one of your apps (only temporary)

management\
   __init__.py
   commands\
      __init__.py
      migrateuser.py

inside migrateuser.py:

from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from yourapp.models import Device #your device model

class Command(BaseCommand):
    def handle(self, *args, **kwargs):
        users = User.objects.all()

        for user in users:
            device = Device.objects.create(name=user.device_id)
            user.device_ids.add(device)

in shell run py manage.py migrateuser.

Remove field device_id and run migrations again.

Leave a comment