当你看此文的时候,默认你才刚刚创建django项目并且没有修改过任何一处的代码(包括settings里的数据库配置)。
默认认为你的settings中的数据库配置为默认的sqllit3,(django自带该数据库,请放心使用)

创建app

python3 manage.py startapp user
首先创建一个新的app
之后到settings里找到INSTALL_APPS中添加”user”

编写User模型

  1. from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
  2. from django.db import models
  3. class UserManager(BaseUserManager):
  4. def _create_user(self, username, password, email, **kwargs):
  5. if not username:
  6. raise ValueError("请传入用户名!")
  7. if not password:
  8. raise ValueError("请传入密码!")
  9. user = self.model(username=username, email=email, **kwargs)
  10. user.set_password(password)
  11. user.save()
  12. return user
  13. def _add_phone(self, username, password, phone):
  14. if not username:
  15. raise ValueError('请输入用户名!')
  16. if not password:
  17. raise ValueError("请传入密码!")
  18. user = self.model(username=username)
  19. if not user.check_password(password):
  20. raise ValueError('密码不正确!')
  21. user.phone = phone
  22. user.save()
  23. return user
  24. def create_user(self, username, password, email, **kwargs):
  25. kwargs['is_superuser'] = False
  26. return self._create_user(username, password, email, **kwargs)
  27. def create_superuser(self, username, password, email, **kwargs):
  28. kwargs['is_superuser'] = True
  29. kwargs['is_staff'] = True
  30. return self._create_user(username, password, email, **kwargs)
  31. def add_phone(self, username, password, phone):
  32. return self._add_phone(username, password, phone)
  33. class User(AbstractBaseUser, PermissionsMixin): # 继承AbstractBaseUser,PermissionsMixin
  34. GENDER_TYPE = (
  35. ("1", "男"),
  36. ("2", "女")
  37. )
  38. username = models.CharField(max_length=15, verbose_name="用户名", unique=True)
  39. nickname = models.CharField(max_length=13, verbose_name="昵称", null=True, blank=True)
  40. age = models.IntegerField(verbose_name="年龄", null=True, blank=True)
  41. gender = models.CharField(max_length=2, choices=GENDER_TYPE, verbose_name="性别", null=True, blank=True)
  42. phone = models.CharField(max_length=11, null=True, blank=True, verbose_name="手机号码", unique=True)
  43. email = models.EmailField(verbose_name="邮箱")
  44. picture = models.ImageField(upload_to="Store/user_picture", verbose_name="用户头像", null=True, blank=True)
  45. home_address = models.CharField(max_length=100, null=True, blank=True, verbose_name="地址")
  46. card_id = models.CharField(max_length=30, verbose_name="身份证", null=True, blank=True)
  47. is_active = models.BooleanField(default=True, verbose_name="激活状态")
  48. is_staff = models.BooleanField(default=True, verbose_name="是否是员工")
  49. date_joined = models.DateTimeField(auto_now_add=True)
  50. USERNAME_FIELD = 'username'
  51. REQUIRED_FIELDS = []
  52. EMAIL_FIELD = 'email'
  53. objects = UserManager()
  54. def get_full_name(self):
  55. return self.username
  56. def get_short_name(self):
  57. return self.username
  58. class Meta:
  59. verbose_name = "用户"
  60. verbose_name_plural = verbose_name

编写完user模型后 先去settings.py中新增一行 AUTH_USER_MODEL = “user.User” # app_name.model_name
python3 manage.py makemigrations user
python3 manage.py migrate
此时数据库中会生成一张名为user_user表 此时就代表我们成功重写了django自带的user表