notice the boolean fields that Django includes in its built-in user model
we can create moreconciseviewswhen we use the inhereited functionality of our built-in user model
we can use python manage.py createsuperuser to add a user
to tweak this buil-in model, we can inherit the functionality of django's builtin AbstractUser and add our own features
AUTH_USER_MODEL = 'user.UserModel'
from django.contrib.auth.models import AbstractUser
classUserModel(AbstractUser):classMeta:
db_table = "my_user"
bio = models.CharField(max_length=256, default='')
using django's built-in auth module, we can register, authorize and authenticate a user in simple steps
from django.contrib.auth import authenticate, login, get_user_model, models
defsign_up_view(request):if request.method == 'GET':
return render(request, 'user/signup.html')
elif request.method == 'POST':
username = request.POST.get('username', None)
password = request.POST.get('password', None)
password2 = request.POST.get('password2', None)
bio = request.POST.get('bio', None)
#queries matching username to check if username is in use
user_exists = get_user_model().objects.filter(username=username)
#if username is in use or password does not matchif user_exists or password!=password2:
return redirect('/sign-up')
else:
UserModel.objects.create_user(username=username, password=password, bio=bio)
return redirect('/sign-in')