Adding Additional Fields to the Custom User Model
Once you have created a custom User model in Django, you may want to enhance it further by adding additional fields. These fields can help capture more user information, making your application more flexible and user-friendly.
Why Add Additional Fields?
Adding fields to your custom User model allows you to store relevant information about your users that are not included in the default User model. For instance, you might want to include:
- Profile information such as a bio or website URL.
- Contact details, like a phone number or address.
- Profile pictures to enhance user experience.
Modifying the Custom User Model
To add additional fields, you simply need to modify the existing custom User model class. Here’s an example that includes a bio and website URL:
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
phone_number = models.CharField(max_length=15, blank=True)
profile_picture = models.ImageField(upload_to='profile_pics/', null=True, blank=True)
bio = models.TextField(blank=True, null=True) # Field for user biography
website = models.URLField(blank=True, null=True) # Field for user website
def __str__(self):
return self.username
Updating the Database
After modifying the model, you need to create a new migration to apply these changes to the database:
python manage.py makemigrations
python manage.py migrate
These commands will create and apply the necessary migrations to add the new fields to the database schema.
Handling User Input in Forms
When you add new fields to your User model, you also need to update any forms that interact with the User model to include these new fields. Here’s an example of how you might modify a user registration form:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = ('username', 'email', 'phone_number', 'bio', 'website') # Include new fields
# You can also specify widgets to customize the form fields
widgets = {
'bio': forms.Textarea(attrs={'rows': 3}),
}
This allows you to collect additional information from users during the registration process.
Admin Interface Adjustments
If you want these new fields to be manageable through the Django admin interface, make sure to update the admin class as well:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
model = CustomUser
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('phone_number', 'profile_picture', 'bio', 'website')}),
)
admin.site.register(CustomUser, CustomUserAdmin)
Next Steps
With the additional fields implemented, ensure that your application logic handles these fields appropriately, especially during user registration and profile updates. In the next subtopic, we will explore how to manage authentication using the custom User model.