#django #django-rest-framework #django-admin #uuid
Вопрос:
Я создал api DRF с Account
моделью, идентифицированной UUID. Я также использую этот UUID в качестве имени пользователя и, следовательно, должен войти на страницу администратора, указав UUID и пароль. Однако, если я попытаюсь это сделать, я получу ошибку
“6c3fe924-1848-483a-8685-c5b095f1” is not a valid UUID.
Что очень странно, так как это создано с использованием UUID.uuid4
. Моя Account
модель выглядит следующим образом
class Account(AbstractBaseUser, PermissionsMixin):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
date_joined = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'id'
objects = AccountManager()
def __str__(self):
return str(self.id)
AccountManager()
Ссылка является пользовательским менеджером, так как эта Account
модель связана с различными типами пользовательских моделей. Код для этого таков
class AccountManager(BaseUserManager):
def create_user(self, password, UUID=None, is_superuser=False):
"""
Create and save a User with the given UUID and password.
"""
if UUID is not None:
user = self.model(id=UUID, is_superuser=is_superuser)
else:
user = self.model(is_superuser=is_superuser)
user.set_password(password)
user.save()
return user
def create_superuser(self, password, UUID=None, **extra_fields):
"""
Create and save a SuperUser with the given UUID and password.
"""
extra_fields.setdefault('is_superuser', True)
# extra_fields.setdefault('is_active', True)
if extra_fields.get('is_superuser') is not True:
raise ValueError(_('Superuser must have is_superuser=True.'))
if UUID is None:
return self.create_user(password, is_superuser=True)
return self.create_user(password, UUID=UUID, is_superuser=True)
Позвольте мне добавить, что все материалы, для которых вы можете передать значение, в UUID
настоящее время не используются. В настоящее время я создаю суперпользователей только с
Account.objects.create_superuser(password="password")
Комментарии:
1. пожалуйста, напишите также менеджеру