Django custom UsernameField form validation not working
So I created a custom form field to validate for duplicate usernames. I'm
using Django + Mongoengine as my database. I have that plugged and working
with the django authentication system so I'm assuming it can be accessed
from forms.py? Maybe that assumption is incorrect. So I have the field
class UsernameField(CharField):
def to_python(self, value):
if not value:
return ""
return value
def validate(self, value):
super(CharField, self).validate(value)
try:
# If the user object exists it won't throw an Error/Exception
user=User.objects.get(username=value)
raise ValidationError("Username already exists")
except:
pass
But when I actually use it in my form, it always seems to validate
correctly even though I've called checked if form.is_valid() is True
No comments:
Post a Comment