In my system I'm trying to auto generate a unique random string for each users to be passed later on in the URL as the reference to that account and I recently read about the UUID in django models. And this is how my model looks like:
class User(AbstractBaseUser, PermissionsMixin):
card_number = models.CharField(max_length=20, unique=True)
first_name = models.CharField(max_length=150)
middle_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
address = models.CharField(max_length=200)
account_code = models.UUIDField(max_length=200, default=uuid.uuid4(), editable=False)
And I ran into an error when start creating superuser in the terminal:
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\ViajeroProject\viaje\manage.py", line 22, in <module>
main()
File "C:\Users\User\PycharmProjects\ViajeroProject\viaje\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
return super().execute(*args, **options)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 113, in handle
error_msg = self._validate_username(username, verbose_field_name, database)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 234, in _validate_username
self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\contrib\auth\base_user.py", line 45, in get_by_natural_key
return self.get(**{self.model.USERNAME_FIELD: username})
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 425, in get
num = len(clone)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 269, in __len__
self._fetch_all()
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\query.py", line 70, in __iter__
for row in compiler.results_iter(results):
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1100, in apply_converters
value = converter(value, expression, connection)
File "C:\Users\User\PycharmProjects\ViajeroProject\venv\lib\site-packages\django\db\backends\sqlite3\operations.py", line 318, in convert_uuidfield_value
value = uuid.UUID(value)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\uuid.py", line 177, in __init__
raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string
How should I address this or is this practice not advisable where I auto generate a unique random string as reference for the user in the URL? Note that users still have their PKs which different from the account_code.
Thank you!
You need to import the module and use default=uuid.uuid4
instead of default=uuid.uuid4()