How do I use error_messages on models in Django -


i understand form documentation http://docs.djangoproject.com/en/dev/ref/models/fields/ can add error_messages model field , supply own dict of error messages. however, keys of dict supposed pass?

class mymodel(models.model):    some_field = models.charfield(max_length=55, error_messages={'required': "my custom error"}) 

if easier on modelform used work, however. rather not have create explicitly creating each field , type again. trying avoid:

class mymodelform(forms.modelform):     some_field = forms.charfield(error_messages={'required' : 'required error'}) 

update 2: test code used in project

my model:

class mytestmodel(models.model):     name = models.charfield(max_length=127,error_messages={'blank' : 'blank','required' : 'required'}) 

my form:

class edittestmodel(modelform):     class meta:         model = mytestmodel 

my view:

tf = edittestmodel({'name' : ''})  print tf.is_valid() # prints false print tf.full_clean() # prints none print tf # prints form, <li> error list containg error "this field required"   <tr><th><label for="id_name">name:</label></th><td><ul class="errorlist"><li>this field required.</li></ul><input id="id_name" type="text" name="name" maxlength="127" /></td></tr> 

you're right, docs not useful. it's recent addition after all!

my guess normal usage of error_messages modelforms, i'd here list of acceptable error keys per field: http://docs.djangoproject.com/en/dev/ref/forms/fields/#error-messages

but, if want safe , not assume anything...

the reliable way going looking @ source @ django/db/models/fields/__init__.py you'll see each of default_error_messages can specified , actual calls self.error_messages['invalid']

# field (base class)  default_error_messages = {     'invalid_choice': _(u'value %r not valid choice.'),     'null': _(u'this field cannot null.'),     'blank': _(u'this field cannot blank.'), }  # autofield       default_error_messages = {         'invalid': _(u'this value must integer.'),     } 

here's doc on model validation: http://docs.djangoproject.com/en/dev/ref/models/instances/#validating-objects

update:

just tested in shell session , appears working. whats up?

i defined simple model:

class subscriptiongroup(models.model):     name = models.charfield(max_length=255, error_messages={'blank': 'invalid!!11', 'null': 'null11!'})  # shell >>> s = subscriptiongroup() >>> s.full_clean() validationerror: {'name': [u'invalid!!11']} 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -