'What is the main difference between clean and full_clean function in Django?

What is the main difference between clean and full_clean function in Django model ?



Solution 1:[1]

they are not against each other usually you call Model.full_clean() to be able to trigger Model.clean() for costume validation for example:

from django.core.exceptions import ValidationError
from django.db import models


class Brand(models.Model):
    title = models.CharField(max_length=512)

    def clean(self):
        if self.title.isdigit():
            raise ValidationError("title must be meaningful not only digits")

    def save(self, *args, **kwargs):
        self.full_clean()
        return super().save(*args, **kwargs)

Solution 2:[2]

here are three steps involved in validating a model:

Validate the model fields - Model.clean_fields()

Validate the model as a whole - Model.clean()

Validate the field uniqueness - Model.validate_unique()

All three steps are performed when you call a model’s full_clean() method. for more information click here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 amirbahador
Solution 2 kamyarmg