Django ValueError: Cannot serialize: <User: username>

When I'm trying to make migrations of my models in Django, I keep getting the same error, even after I've commented out all the changes:

(.venv) C:\Users\jezdo\venv\chat\chat_proj>python manage.py makemigrations chat
Migrations for 'chat':
  chat\migrations\0002_alter_customusergroup_custom_group_name_and_more.py
    - Alter field custom_group_name on customusergroup
    - Alter field users on customusergroup
Traceback (most recent call last):
  File "C:\Users\jezdo\.venv\chat\chat_proj\manage.py", line 22, in <module>
    main()
  File "C:\Users\jezdo\.venv\chat\chat_proj\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\jezdo\.venv\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "C:\Users\jezdo\.venv\lib\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\jezdo\.venv\lib\site-packages\django\core\management\base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\jezdo\.venv\lib\site-packages\django\core\management\base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "C:\Users\jezdo\.venv\lib\site-packages\django\core\management\base.py", line 96, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\jezdo\.venv\lib\site-packages\django\core\management\commands\makemigrations.py", line 239, in handle
    self.write_migration_files(changes)
  File "C:\Users\jezdo\.venv\lib\site-packages\django\core\management\commands\makemigrations.py", line 278, in write_migration_files
    migration_string = writer.as_string()
  File "C:\Users\jezdo\.venv\lib\site-packages\django\db\migrations\writer.py", line 141, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "C:\Users\jezdo\.venv\lib\site-packages\django\db\migrations\writer.py", line 99, in serialize
    _write(arg_name, arg_value)
  File "C:\Users\jezdo\.venv\lib\site-packages\django\db\migrations\writer.py", line 63, in _write
    arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
  File "C:\Users\jezdo\.venv\lib\site-packages\django\db\migrations\writer.py", line 282, in serialize
    return serializer_factory(value).serialize()
  File "C:\Users\jezdo\.venv\lib\site-packages\django\db\migrations\serializer.py", line 221, in serialize
    return self.serialize_deconstructed(path, args, kwargs)
  File "C:\Users\jezdo\.venv\lib\site-packages\django\db\migrations\serializer.py", line 99, in serialize_deconstructed
    arg_string, arg_imports = serializer_factory(arg).serialize()
  File "C:\Users\jezdo\.venv\lib\site-packages\django\db\migrations\serializer.py", line 50, in serialize
    item_string, item_imports = serializer_factory(item).serialize()
  File "C:\Users\jezdo\.venv\lib\site-packages\django\db\migrations\serializer.py", line 50, in serialize
    item_string, item_imports = serializer_factory(item).serialize()
  File "C:\Users\jezdo\.venv\lib\site-packages\django\db\migrations\serializer.py", line 386, in serializer_factory
    raise ValueError(
ValueError: Cannot serialize: <User: jezdo>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/4.1/topics/migrations/#migration-serializing

my model in models.py:

class CustomUserGroup(models.Model):
    custom_group_name = models.CharField(max_length=50, unique=True)
    users= MultiSelectField(max_length=100,choices=users_list,unique=False)

    class Meta:
        verbose_name_plural = 'Custom Groups'
        ordering = ['custom_group_name']

    def __unicode__(self):
        return self.custom_group_name

I wanted to create another model similar to CustomUserGroup byt with a different "users" field:

class CustomUserGroup2(models.Model):
    custom_group_name = models.CharField(max_length=50, unique=True)
    users= models.ManyToManyField(User)

    class Meta:
        verbose_name_plural = 'Custom Groups'
        ordering = ['custom_group_name']

    def __unicode__(self):
        return self.custom_group_name

but couldn't makemigrations due to the described error. Now I cannot make any migrations whatsoever, even after having deleted the CustomUserGroup2 class.

I'm using Python 3.10.4 and Django 4.1.6.

Back to Top