Custom User Flashcards

1
Q

Create custom User Model that inherits from AbstractUser

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Null vs blank

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In forms.py

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly