M
M
maxclax2015-03-15 12:25:21
Django
maxclax, 2015-03-15 12:25:21

Why doesn't Django's clean() pass validation in tests?

I have a model and a method in it

def clean(self):
        # проверка на совпадение входной и выходной валюты
        if self.ccy == self.base_ccy:
            raise ValidationError(_('course.model.clean.unique'))
        # проверка на обратное направления
        if Course.objects.filter(ccy=self.base_ccy, base_ccy=self.ccy):
            raise ValidationError(_('course.model.clean.reunique'))

when passing tests in this form, it does not pass due to the fact that it does not call clean:
# проверка на добавление обекта с одинаковым направлением
        with self.assertRaises(ValidationError):
            Course.objects.create(ccy=self.course_1.base_ccy, base_ccy=self.course_1.base_ccy)

have to add .clean() at the end. What's wrong? Why is the method not called by default?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Shikanov, 2015-03-15
@dizballanze

From documentation :

Note, however, that like Model.full_clean(), a model's clean() method is not invoked when you call your model's save() method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question