django check if user already exists code example
Example: check if username exists in database django
class FormClass:
username = ...
def clean(self):
super(FormClass, self).clean()
username = self.cleaned_data['username']
if User.objects.exclude(pk=self.instance.pk).filter(username=username).exists():
raise forms.ValidationError(f'Username "{username}" is already in use.')
return username