Custom User Flashcards
Create custom User Model that inherits from AbstractUser
accounts/models.py from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
age = models.PositiveIntegerField(null=True, blank=True)
Null vs blank
null is database -related. When a field has null=True it can store a database entry as NULL, meaning no value.
• blank is validation-related. If blank=True then a form will allow an empty value, whereas if blank=False then a value is required.
In forms.py
from django.contrib.auth.forms import UserCreationForm, UserChangeForm from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = CustomUser
fields = UserCreationForm.Meta.fields + (“age”,)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields