Where to put validator for Django model field? -


the django documentation shows example of custom validator model field. doc doesn't put validator function in code. if put in models.py file in either location (as shown below), error when try start server:

nameerror: name 'validate_name' not defined 

where right location validate_name function? thanks.

# models.py, example 1 django.db import models django.core.exceptions import validationerror  class commentmodel(models.model):     # error if define validator here     def validate_name(value):         if value == '':             raise validationerror(u'%s cannot left blank' % value)      name = models.charfield(max_length=25, validators=[validate_name])   # models.py, example 2 django.db import models django.core.exceptions import validationerror  # error if define validator here def validate_name(value):     if value == '':         raise validationerror(u'%s cannot left blank' % value)  class commentmodel(models.model):     name = models.charfield(max_length=25, validators=[validate_name]) 

declaring validation method within models file outside class declaration (example 2) should work fine

you may getting nameerror if you're using function within file without importing method first


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -