Closed henadzit closed 1 month ago
Hi @abondar! This PR contains a non backwards compatible change. How should I treat the changelog? Should I introduce 0.22.1
there with the note of this change as part of this PR? Thanks!
Totals | |
---|---|
Change from base Build 11435130298: | -0.04% |
Covered Lines: | 5990 |
Relevant Lines: | 6613 |
Hi!
Why you have decided to go with approach of turning off validation, rather than just making sure that annotated field instance doesn't have validators?
Seems like if we change this line From
if field in self.annotations:
annotation = self.annotations[field]
field_object = getattr(annotation, "field_object", None)
if field_object:
return field_object.to_python_value
return lambda x: x
To
if field in self.annotations:
annotation = self.annotations[field]
field_object = getattr(annotation, "field_object", None)
if field_object:
new_field_class = deepcopy(field_object)
new_field_class.validators = []
return new_field_class.to_python_value
return lambda x: x
We can exterminate root problem, of validators leaking to dynamically generated fields. And that way we won't have to introduce breaking changes
@abondar thank you for reviewing and challenging my approach!
I gave it a bit more thought and realized that validation on loading records from the database is not common. I prepared another pull request, please have a look!
Description
Field.to_python_value
with thevalidate
argument with the default valueTrue
which regulates whether validation should be run.Field
and overrodeto_python_value
because its signature changedMotivation and Context
When an arithmetic expressions on a model field is used in annotations, the field's
to_python_value
is used to process the database value.to_python_value
is running validations by default and if the result of aggregation function doesn't pass field's validation, an exception happens.will cause
tortoise.exceptions.ValidationError: max_value: Value should be less or equal to 20.0
Other ideas
At the moment the validation is being run when the object is being loaded from the database, for instance, here and here. Does it really need to happen? The introduction of the
validate
argument can be used to disable validation in these cases.How Has This Been Tested
Checklist: