简体   繁体   中英

Django RegexValidator fails on empty string

I have added a custom validation for my username field,

my regex is, ^[a-zA-Z0-9_]{1,15}$ which I need to validate az, 0-9 and _ (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 is validated on the save() ,

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

    super(AbstractBaseUser, self).save(**kwargs)

This works perfectly on the Django shell,

>>> from django.core.validators import RegexValidator
>>> valid_username = r'^[a-zA-Z0-9_]{1,15}$'
>>> validator = RegexValidator(regex=valid_username, message='alphanumerics and underscores are 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 and underscores are allowed [1-15].']

but when I try to create a user with shell with username as "" .

>>> 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 it allows me to create a user with empty string as the user name, the second time it raises the integrity error. I want the system to raise integrity error on "" as the username.

What am I doing wrong?

The validator is never called in your save method when the string is empty:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM