Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is a django validator function's return value stored?

In my django app, this is my validator.py

from django.core.exceptions import ValidationError
from django.core.validators import URLValidator

def validate_url(value):
    url_validator = URLValidator()
    url_invalid = False
    try:
        url_validator(value)
    except:
        url_invalid = True
        try:
            value = "http://"+value
            url_validator(value)
            url_invalid = False
        except:
            url_invalid = True

    if url_invalid:
        raise ValidationError("Invalid Data for this field")
    return value

which is used to validate this :

from django import forms
from .validators import validate_url
class SubmitUrlForm(forms.Form):
    url = forms.CharField(label="Submit URL",validators=[validate_url])

When I enter URL like google.co.in, and print the value right before returning it from validate_url, it prints http://google.co.in but when I try to get the cleaned_data['url'] in my views, it still shows google.co.in. So where does the value returned by my validator go and do I need to explicitly edit the clean() functions to change the url field value??

The doc says the following:

The clean() method on a Field subclass is responsible for running to_python(), validate(), and run_validators() in the correct order and propagating their errors. If, at any time, any of the methods raise ValidationError, the validation stops and that error is raised. This method returns the clean data, which is then inserted into the cleaned_data dictionary of the form.

I am still not sure where the validator return value goes and if it is possible to change cleaned_data dict using the validator.

like image 711
Krash Avatar asked Apr 15 '17 07:04

Krash


People also ask

How does validation work in Django?

Django provides built-in methods to validate form data automatically. Django forms submit only if it contains CSRF tokens. It uses uses a clean and easy approach to validate data. The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class.

What does validators do in the code field validators?

A validator is a function that takes in the fields new value, and then acts on it. They are a simple way to customize a field. They allow you to trigger functionality when a field's value changes, modify input, or limit which values are acceptable.

How does Django validate email?

Email-Verification for Django. Email verification for new signups or new users is a two-step verification process and adds a layer for security for valid users. verify_email is a django app that provides this functionality right of the bat without any complex implementation.


1 Answers

From the docs:

A validator is merely a callable object or function that takes a value and simply returns nothing if the value is valid or raises a ValidationError if not.

The return value is simply ignored.

If you want to be able to modify the value you may use clean_field on the forms as described here:

class SubmitUrlForm(forms.Form):
    url = ...
    def clean_url(self):
        value = self.cleaned_data['url']
        ...
        return updated_value

Validators are only about validating the data, hence that is why the return value of the validator gets ignored.

You are looking for data "cleaning" (transforming it in a common form). In Django, Forms are responsible for data cleaning.

like image 92
Michael Rigoni Avatar answered Oct 17 '22 01:10

Michael Rigoni