CodeLover
CodeLover

Reputation: 97

How to write custom Username validation using Self.clean_username for Profile Update form in Django?

I am a beginner and I'm still learning Django. I am writing my custom form validation for user profile update. My profile update form has -

First I have used default form validation. It is also good but I want to update the error message so I tried to create a custom validation function as I seen in a post in Stack Overflow. Please also check my username validation in forms.py

So what is the error now?

Now I am facing one more issues

  1. If I click on update button without changing anything then it shows can't use this username. {username is set to unique=True in model fields}
  2. If I don't update username and update just first name and last name. It doesn't allow this says same error. "Username is already taken" {because that username is validated and it already taken by current user"}

What exactly I want?

Instead of changing just error message now I want to write my own validation for this. I want if some click on update without changing any data no error message should be shown.

I can update each field independently. No error message should be shown if i don't update username.

My Views

    
@login_required()
def profile(request):
    
    if request.method=='POST':
     
        u_form = UserUpdateForm(request.POST,request.FILES,instance=request.user)
        if u_form.is_valid():
            u_form.save()
            
            messages.success(request, f'Profile Updated Succesfully')
            redirect('profile/')
    else:
        u_form = UserUpdateForm(instance=request.user)
       
    notes = UserCreatedNote.objects.filter(user=request.user)
    context = {'u_form': u_form,'notes':notes}

    
    return render(request,'profile.html',context)

My models

from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)

# ///////////////User Manager/////////////////////
# Create your models here.
# overriding the create and superuser funciton

class MyAccountManager(BaseUserManager):
    def create_user(self,email,username,password=None):
        if not email:
            raise ValueError("Users Must Have email Address")
        if not username:
            raise ValueError("Users Must Have username")
        user = self.model(
            email=self.normalize_email(email),
            username=username,

        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self,email,username,password):
        user = self.create_user(
            email = self.normalize_email(email),
            username=username,
            password=password,
        )
        user.is_admin = True
        user.is_staff = True 
        user.is_superuser = True
        user.save(using=self._db)
        return user
         

# ////////////////////////////////////////////////////////////
def get_profile_image_filepath(self,filename):
    return f'profile_images/{self.pk}/{filename}' #pk= primary key


def get_default_profile_image():
    return "img/default_profile/default.png"

class KeepSafeUserModel(AbstractBaseUser):
    first_name = models.CharField(verbose_name='first_name',max_length=30,default="")
    last_name = models.CharField(verbose_name='last_name',max_length=30,default="")
    email= models.TextField(verbose_name='email',max_length=60,unique=True,primary_key=True)
    username = models.CharField(max_length=30,unique=True)
    date_joined = models.DateTimeField(verbose_name="date joined",auto_now_add=True)
    last_login = models.DateTimeField(verbose_name='last_login',auto_now=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    profile_image = models.ImageField(max_length=225,upload_to=get_profile_image_filepath,null=True,blank=True,default=get_default_profile_image)
    #password field is buil-in
   

    objects = MyAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']
    
    def __str__(self):
        return self.username
    
    def has_perm(self,perm,obj=None):
        return self.is_admin

    def has_module_perms(self,app_Label):
        return True
        
    def get_profile_image_filename(self):
        return str(self.profile_image)[str(self.profile_image).index('profile_images/{self.pk}/')]
        

forms.py

class UserUpdateForm(forms.ModelForm):

    profile_image = forms.ImageField(required=False,error_messages ={'invalid':("Image files only")},widget=forms.FileInput)
    class Meta:
        model = KeepSafeUserModel
        fields = ['username','first_name','last_name','profile_image']
        labels ={
            'username':'Username',
            'first_name':'First Name',
            'last_name':'Last Name',
            'profile_image':'Profile Image'
        }
    def clean_username(self):
        username = self.cleaned_data['username']
        
        if not username.isdigit():
            users = KeepSafeUserModel.objects.filter(username__iexact=username)
            if users:
                raise forms.ValidationError("Username has already taken by someone else")
        else:
            raise forms.ValidationError("Username can't contains only numbers")
        return username

Upvotes: 1

Views: 1606

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477338

The problem is that if you update your KeepSafeUserModel, you will have a hit for KeepSafeUserModel.objects.filter(username__iexact=username): indeed, it will simply match with itself.

You can simply exclude the object with:

def clean_username(self):
    username = self.cleaned_data['username']
    
    if not username.isdigit():
        users = KeepSafeUserModel.objects.exclude(pk=self.instance.pk).filter(
            username__iexact=username
        ).exists()
        if users:
            raise forms.ValidationError"Username has already taken by someone else")
    else:
        raise forms.ValidationError("Username can't contains only numbers")
    return username

Upvotes: 2

Fran Na Jaya
Fran Na Jaya

Reputation: 388

I think you already pass the instance of user logged in to the form. So you need to make the clean_username(self) validation become like this.

def clean_username(self):
    username = self.cleaned_data['username']

    if not username.isdigit():
        users = KeepSafeUserModel.objects.filter(username__iexact=username)
        if users:
            if users.username != self.instance.username:
                raise forms.ValidationError("Username has already taken by someone else")
    else:
        raise forms.ValidationError("Username can't contains only numbers")
    return username

It will validate the username given and stored is not same

Upvotes: 1

Related Questions