Is there any connection between the clean clean () method and the clean () method?

I rewrote the methods clean()for some of my models to create constraints to meet my database schema requirements (since these checks required runtime information).

Since I finished most of the back components (models, signals, ..), now I am trying to write ModelFormfor my models.

I am wondering if there is any connection between the clean()model method and the clean()implementation on the form side?

If this is the case and the form clean()invokes the model clean(), I will not have to rewrite the implementation of the model clean()and avoid code redundancy.

+4
source share
2 answers

Yes, ModelFormcleaning includes cleaning the model. Idea c ModelForm: There are many useful defaults that can be determined by automatically creating a form object from the model.

I discovered this clean chain through personal experience, but I can link to the source to back it up.

In 1.8, ModelFormcall the model instance method full_clean. In 1.7, it calls the method cleandirectly.

Form.full_clean ()

def full_clean(self):
    # ..... snip
    self._clean_fields()
    self._clean_form()
    self._post_clean()

ModelForm._post_clean for 1.8

The model full_clean()calls clean()among other validations: https://docs.djangoproject.com/en/1.8/ref/models/instances/

self.instance.full_clean(exclude=exclude, validate_unique=False)

ModelForm._post_clean for 1.7

self.instance.clean()
+3
source

Model.clean ModelForm clean, , - .

clean , .

Model.clean , , , .

.

ModelForm clean, , , , , .

clean() , , .

:

clean() , , .

, - , :

, clean() , clean() . , . - , - .

+1

Source: https://habr.com/ru/post/1585004/


All Articles