Django Custom Validators Not Working

Sri Harsha

I wrote a custom validator, which will raise ValidationError if given field value is negative.

  def validate_positive(value):
        if value < 0:
            raise ValidationError(
                    _('%(value) is negative number'),
                    params = {'value': value}
                    )

i added this to my model field via the field’s validators argument

class Book(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    title = models.CharField(max_length=50)
    price = models.IntegerField(default=0,validators=[validate_positive])
    quantity = models.IntegerField(default=0,validators=[validate_positive])

But while creating object it's not raising any error if price is less than zero.
I don't know where i am doing wrong and i am new to django.
I am using Django 1.9.
Please help me .

Sayse

Validators are used for forms, not for creating an object. If you're creating an object outside of a form then you need to provide an alternative way to validate input.

The easiest way to do this is to call the model's full_clean method before saving as shown in the docs

from django.core.exceptions import ValidationError
try:
    article.full_clean()
except ValidationError as e:
    # Do something based on the errors contained in e.message_dict.
    # Display them to a user, or handle them programmatically.
    pass

This is similar to what would happen from a form, and would call any validators on your model fields.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CausesValidation not working with custom validators

From Dev

Django: Python path syntax to my custom validators.py?

From Dev

Django - Custom tag not working

From Dev

CustomValidator not working with other validators

From Dev

CustomValidator not working with other validators

From Dev

Testing custom validators with Minitest

From Dev

Spring MVC custom validators

From Dev

Proptypes custom validators with Flow

From Dev

django field is required validators

From Java

Django: How to decide between class-based and function-based custom validators?

From Java

Custom pagination in Django is not working properly

From Dev

django admin not working with custom user

From Dev

Custom formset validation not working in django

From Dev

The Validators And Errors Are Not Working In Angular 9

From Dev

Custom validators in WTForms using Flask

From Dev

Django login form not working for custom user model

From Dev

django 1.8 custom validation of form not working

From Dev

django authentication with Custom User model not working

From Dev

django-allauth with Custom User Model not working

From Dev

custom authentication not working in django-tastypie

From Dev

Angular 4 : Validators.pattern() not working

From Dev

Abstract Factory for custom Validators - options discarded?

From Dev

(Laravel) How to add errors in custom validators?

From Dev

Creating dependant custom validators for form inputs?

From Dev

Custom Angular Validators- Passing an Argument

From Dev

Create custom validators for dynamic forms angular 2

From Dev

Angular 4 - Using Asynchronous custom validators

From Dev

Passing Model Field Data to Validators in Django

From Dev

Override default django username validators for AbstractUser model

Related Related

  1. 1

    CausesValidation not working with custom validators

  2. 2

    Django: Python path syntax to my custom validators.py?

  3. 3

    Django - Custom tag not working

  4. 4

    CustomValidator not working with other validators

  5. 5

    CustomValidator not working with other validators

  6. 6

    Testing custom validators with Minitest

  7. 7

    Spring MVC custom validators

  8. 8

    Proptypes custom validators with Flow

  9. 9

    django field is required validators

  10. 10

    Django: How to decide between class-based and function-based custom validators?

  11. 11

    Custom pagination in Django is not working properly

  12. 12

    django admin not working with custom user

  13. 13

    Custom formset validation not working in django

  14. 14

    The Validators And Errors Are Not Working In Angular 9

  15. 15

    Custom validators in WTForms using Flask

  16. 16

    Django login form not working for custom user model

  17. 17

    django 1.8 custom validation of form not working

  18. 18

    django authentication with Custom User model not working

  19. 19

    django-allauth with Custom User Model not working

  20. 20

    custom authentication not working in django-tastypie

  21. 21

    Angular 4 : Validators.pattern() not working

  22. 22

    Abstract Factory for custom Validators - options discarded?

  23. 23

    (Laravel) How to add errors in custom validators?

  24. 24

    Creating dependant custom validators for form inputs?

  25. 25

    Custom Angular Validators- Passing an Argument

  26. 26

    Create custom validators for dynamic forms angular 2

  27. 27

    Angular 4 - Using Asynchronous custom validators

  28. 28

    Passing Model Field Data to Validators in Django

  29. 29

    Override default django username validators for AbstractUser model

HotTag

Archive