Home > front end >  Django Admin doesn't recognise blank=True in model
Django Admin doesn't recognise blank=True in model

Time:12-21

When I try to edit a user (using a custom UserChangeForm) in the Django admin panel, validation insists that fields I have set blank=True in the model are required.

I don't know where to begin solving this; I had the same issue with the CustomUserCreationForm but reverted to using the default which works as expected (asks for username, password1 & password2, creates the user with blank display_name, bio and profile_picture fields).

models.py:

from django.db import models
from django.contrib.auth.models import AbstractUser


class CustomUser(AbstractUser):
    display_name = models.CharField(max_length=30, blank=True, null=True)
    bio = models.TextField(blank=True, null=True)
    profile_picture = models.ImageField(upload_to='images/', blank=True, null=True)
    
    def save(self, *args, **kwargs):
        if not self.display_name:
            self.display_name = self.username
        super().save(*args, **kwargs)

    def __str__(self):
        return self.username

forms.py:

from django import forms
from django.contrib.auth.forms import UserChangeForm
from .models import CustomUser

class CustomUserChangeForm(UserChangeForm):
    display_name = forms.CharField(label="display_name")
    bio = forms.CharField(widget=forms.Textarea)
    profile_picture = forms.ImageField(label="profile_picture")

    class Meta():
        model = CustomUser
        fields = ("username", "email", "display_name", "bio", "profile_picture")

admin.py:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from .forms import CustomUserChangeForm
from .models import CustomUser

class CustomUserAdmin(UserAdmin):
    form = CustomUserChangeForm

    fieldsets = (
        (None, 
            {'fields': ('username', 'password', 'email', 'display_name', 'bio', 'profile_picture')}
        ),
    )
    model = CustomUser
    list_display = ["username", "email",]

admin.site.register(CustomUser, CustomUserAdmin)

CodePudding user response:

From the Django documentation:

By default, each Field class assumes the value is required, so if you pass an empty value – either None or the empty string ("") – then clean() will raise a ValidationError exception:

So you have to add required=False in your forms.py. For example:

display_name = forms.CharField(required=False, label="display_name")
  • Related