По видео уроку создавал свою модель пользователей и вроде все правильно, но возникает ошибка. Смотрел в гугле, там писали, что, возможно, в базе уже есть записи этой модели, но я создавал новый проект и миграции делал только после того написал кастомную модель.
Трейсбек:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "c:\python36\project\venv\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
utility.execute()
File "c:\python36\project\venv\lib\site-packages\django\core\management\__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "c:\python36\project\venv\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "c:\python36\project\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 63, in execute
return super(Command, self).execute(*args, **options)
File "c:\python36\project\venv\lib\site-packages\django\core\management\base.py", line 330, in execute
output = self.handle(*args, **options)
File "c:\python36\project\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 183, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
File "c:\python36\project\lolts\cabinet\models.py", line 31, in create_superuser
is_admin = True)
File "c:\python36\project\lolts\cabinet\models.py", line 17, in create_user
user_obj.save(using=self._db)
File "c:\python36\project\venv\lib\site-packages\django\contrib\auth\base_user.py", line 80, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "c:\python36\project\venv\lib\site-packages\django\db\models\base.py", line 806, in save
force_update=force_update, update_fields=update_fields)
File "c:\python36\project\venv\lib\site-packages\django\db\models\base.py", line 836, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "c:\python36\project\venv\lib\site-packages\django\db\models\base.py", line 903, in _save_table
forced_update)
File "c:\python36\project\venv\lib\site-packages\django\db\models\base.py", line 933, in _do_update
filtered = base_qs.filter(pk=pk_val)
File "c:\python36\project\venv\lib\site-packages\django\db\models\query.py", line 784, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "c:\python36\project\venv\lib\site-packages\django\db\models\query.py", line 802, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "c:\python36\project\venv\lib\site-packages\django\db\models\sql\query.py", line 1261, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "c:\python36\project\venv\lib\site-packages\django\db\models\sql\query.py", line 1287, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "c:\python36\project\venv\lib\site-packages\django\db\models\sql\query.py", line 1221, in build_filter
condition = self.build_lookup(lookups, col, value)
File "c:\python36\project\venv\lib\site-packages\django\db\models\sql\query.py", line 1115, in build_lookup
return final_lookup(lhs, rhs)
File "c:\python36\project\venv\lib\site-packages\django\db\models\lookups.py", line 24, in __init__
self.rhs = self.get_prep_lookup()
File "c:\python36\project\venv\lib\site-packages\django\db\models\lookups.py", line 74, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "c:\python36\project\venv\lib\site-packages\django\db\models\fields\__init__.py", line 966, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'admin'
Модель:
class UserManager(BaseUserManager):
def create_user(self, login, about=None, password=None, is_staff=False, is_active=True, is_admin=False):
if not login:
raise ValueError("Вы обязаны иметь логин, будет прекрасно если он будет совпадать с вашим игровым ником.")
if not password:
raise ValueError("Ну, вы чего? А пароль кто за вас введет?")
user_obj = self.model(login)
user_obj.set_password(password)
user_obj.about = about
user_obj.staff = is_staff
user_obj.admin = is_admin
user_obj.active = is_active
user_obj.save(using=self._db)
return user_obj
def create_staffuser(self, login, password=None):
user = self.create_user(
login,
password = password,
is_staff = True)
return user
def create_superuser(self, login, password=None):
user = self.create_user(
login,
password=password,
is_staff= True,
is_admin = True)
return user
class CustomUser(AbstractBaseUser):
login = models.CharField(max_length=255, blank=False, unique=True)
about = models.TextField(blank=True)
active = models.BooleanField(default=True)
admin = models.BooleanField(default=False)
staff = models.BooleanField(default=False)
USERNAME_FIELD = 'login'
objects = UserManager()
def __str__(self):
return self.login
def get_name(self):
return self.login
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_active(self):
return self.active
@property
def is_admin(self):
return is_admin
@property
def is_staff(self):
return self.staff
class GuestLogin(models.Model):
login = models.CharField(max_length=255)
active = models.BooleanField(default=True)
def __str__(self):
return self.login