Saturday, August 29, 2015

Re: Models.clean() how to raise ValidationError WITH error_code and show field_based errors in the Form-Template?

You are raising a dict style ValidationError.  To get the actual fields you need to use the_exception.error_dict['note'].error_list[0].code.  

On Jul 30, 2015, at 4:10 AM, ThomasTheDjangoFan <stefan.eichholz.berlin@googlemail.com> wrote:

class TestMyNotebookModelTest(unittest):
    
def test_clean(self):
        
# I'd love to be able to do a test like this one
        # and valide the error code!

        # Lets get an error-message
        
with self.assertRaises(ValidationError) as test:
            model 
= MyNotebookModel()
            model
.note = 'Now this is a valid one. P.S. I love django!'
            model
.name = 'Britney is gonna fail - Hit me Baby One More Time!'
            model
.clean()
            model
.save()
        
# validate that the error messages has the right code
        the_exception 
= test.exception
        
self.assertEqual(the_exception.error_code, 2)

Peter of the Norse



No comments:

Post a Comment