Forced Django Forms

What does the dango forms of coercion argument? I read the documentation, but its not very useful, so it would be useful to clarify a few usage examples. To quote the documentation:

A function that takes one argument and returns a forced value. Examples include int, int, float, bool, and other types. By default, the authentication function is used.

+4
source share
1 answer

TypedChoiceField is exactly like ChoiceField, except that ChoiceField always returns unicode.

With TypedChoiceField, you pass a function that takes one argument and returns a value other than the type you want. For example, if you want to force a value to an integer, use:

int_field = forms.TypedChoiceField(choices=SOME_CHOICES, coerce=int) 

The field value will always be integer or invalid.

+6
source

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


All Articles