python - Django RegexValidator fails on empty string -


i have added custom validation username field,

my regex is, ^[a-za-z0-9_]{1,15}$ need validate a-z, 0-9 , _ (underscores) of length 1-15.

def validate_username(value):     valid_username = r'^[a-za-z0-9_]{1,15}$'     validator = regexvalidator(regex=valid_username, message='a-z0-9 [1-15].', code='invalid name')     validator(value)     return value 

which validated on save(),

def save(self, **kwargs):     if self.username:         self.username = validate_username(self.username)      super(abstractbaseuser, self).save(**kwargs) 

this works on django shell,

>>> django.core.validators import regexvalidator >>> valid_username = r'^[a-za-z0-9_]{1,15}$' >>> validator = regexvalidator(regex=valid_username, message='alphanumerics , underscores allowed [1-15].',                                code='invalid name') >>> validator('') traceback (most recent call last):   file "<input>", line 1, in <module>   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/core/validators.py", line 61, in __call__     raise validationerror(self.message, code=self.code) validationerror: [u'alphanumerics , underscores allowed [1-15].'] 

but when try create user shell username "".

>>> x = user.objects.create(email="me@localhost.com", username="") >>> x.username '' >>> x = user.objects.create(email="me@localhosted.com", username="") traceback (most recent call last):   file "<input>", line 1, in <module>   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method     return getattr(self.get_queryset(), name)(*args, **kwargs)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/models/query.py", line 401, in create     obj.save(force_insert=true, using=self.db)   file "/home/marty/projects/cleo/applications/auth/users/models.py", line 85, in save     super(abstractbaseuser, self).save(**kwargs)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/models/base.py", line 708, in save     force_update=force_update, update_fields=update_fields)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/models/base.py", line 736, in save_base     updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/models/base.py", line 820, in _save_table     result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/models/base.py", line 859, in _do_insert     using=using, raw=raw)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method     return getattr(self.get_queryset(), name)(*args, **kwargs)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/models/query.py", line 1039, in _insert     return query.get_compiler(using=using).execute_sql(return_id)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql     cursor.execute(sql, params)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute     return super(cursordebugwrapper, self).execute(sql, params)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute     return self.cursor.execute(sql, params)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__     six.reraise(dj_exc_type, dj_exc_value, traceback)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute     return self.cursor.execute(sql, params)   file "/home/marty/.virtualenvs/jk/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 323, in execute     return database.cursor.execute(self, query, params) integrityerror: unique constraint failed: users_user.username 

the first time allows me create user empty string user name, second time raises integrity error. want system raise integrity error on "" username.

what doing wrong?

the validator never called in save method when string empty:

if self.username: # empty string not pass condition     self.username = validate_username(self.username) 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -