python - How can I override username validation in Django 1.11? -


i have a custom user model in django 1.11 site, along django-registration.

on registration form, prompt username , passphrase. have added minimumlengthvalidator , custom wordcountvalidator validation passphrase field in settings.py (see below) -- these validation rules rendered in registration form automatically.

now want change username validation 3 rules: length, must start letter, may contain alphanumeric characters. don't mind combining these single rule.

i tried adding username_validator class , setting that, nothing happens -- because i'm inheriting abstractuser not user (related 1.10 bug?).

how can use own validation rule (preferably multi-clause) on username field?

simplified appearance of form:

username: [     ]  required. 150 characters or fewer. letters, digits , @/./+/-/_ only.  passphrase: [     ]  passphrase must contain @ least 15 characters. passphrase must contain @ least 3 words. use spaces between words.  passphrase confirmation: [       ]  enter same passphrase before, verification. 

settings.py

auth_password_validators = [     {         'name': 'django.contrib.auth.password_validation.minimumlengthvalidator',         'options': {             'min_length': 15,         }     },     {         'name': 'custom.validators.wordcountvalidator',         'options': {             'min_words': 3,         }     }, ] 

models.py

class user(abstractuser):     objects = usermanager()      def __init__(self, *args, **kwargs):          user = super(user, self).__init__(*args, **kwargs)         return user      ... 

validators.py

class wordcountvalidator(object):     ...  class startlettervalidator(object):     ...  class alphanumericvalidator(object):     ...  class combinedvalidator(object):     ... 

overwrite clean() method of usermodel , call validators there:

def clean(self):     # data-clean     # maybe super().clean() here      validator1 = wordcountervalidator(self)     validator1.myvalidationmethod()     #...     validator2 = startlettervalidator(self)     validator2.myvalidationmethod()     #...     validator3 = alphanumericvalidator(self)     validator3.myvalidationmethod()     #...     validator4 = combinedvalidator(self)     validator4.myvalidationmethod()     #...      # maybe super().clean() here 

to simplify validation code, might useful write validation methods instead of validation classes. think both designs do.

update

extended answer question in comment, due size of answer:

django provides validationerror this. if want raise multiple validationerrors @ once suggest use dictionary collect errors:

err_dict = {} 

then add error dict field-name key , error message value: err_dict["field-name"] = "error mssg".

at end of clean() check dict errors if err_dict (true if error[s] in dict) , raise raise validationerror(err_dict).

in case might want like

err_dict = validatot1.myvalidationmethod(err_dict) 

to extend err_dict (if necessary) each validation-function.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -