Field¶
Base class: Part
Class that describes a field, i.e. what input controls to render, the label, etc.
See Form for more complete examples.
The life cycle of the data is:
raw_data: will be set if the corresponding key is present in the HTTP request
parsed_data: set if parsing is successful, which only happens if the previous step succeeded
value: set if validation is successful, which only happens if the previous step succeeded
Note that, in addition to the parameters with the defined behavior below, you can pass in any keyword argument you need yourself, including callables that conform to the protocol, and they will be added and evaluated as members.
All these parameters can be callables, and if they are, will be evaluated with the keyword arguments form and field. The only exceptions are is_valid (which gets form, field and parsed_data), render_value (which takes form, field and value) and parse (which gets form, field, string_value). Example of using a lambda to specify a value:
Field(attrs__id=lambda form, field: 'my_id_%s' % field._name)
Refinable members¶
after (evaluated)¶
Set the order of fields, see the howto for an example.
Type: Union[int, str]
See after
assets¶
Type: Namespace
See assets
attr (evaluated)¶
The attribute path to apply or get the data from. For example using foo__bar__baz will result in your_instance.foo.bar.baz being set by the apply() function. Setting this to None will mean no attribute is read or written by apply(). Defaults to same as name.
Type: str
See attr
attrs (evaluated)¶
A dict containing any custom html attributes to be sent to the input__template.
Type: Attrs
See attributes
choice_display_name_formatter¶
Callback given the keyword argument choice in addition to standard parameters, to obtain the display name representing a given choice to the end user. Default implementation will use str(choice)
Type: Callable[..., str]
Default: lambda choice, **_: '%s' % choice
choice_id_formatter¶
Callback given the keyword argument choice in addition to standard parameters, to obtain the string value to represent the identity of a given choice. Default implementation will use str(choice)
Type: Callable[..., str]
Default: lambda choice, **_: '%s' % choice
choice_to_optgroup¶
Callback to generate the optgroup for the given choice. It will get the keyword argument choice. It should return None if the choice should not be grouped.
Type: Optional[Callable[..., Optional[str]]]
choices (evaluated)¶
Type: Callable[..., List[Any]]
display_name (evaluated)¶
The text in the HTML label tag. Default: capitalize(name).replace('_', ' ')
Type: str
See name
editable (evaluated)¶
Is this field editable.
Type: bool
Default: True
empty_label (evaluated)¶
Type: str
Default: ---
endpoints¶
Type: Namespace
See endpoints
errors¶
Type: Errors
- Cookbook:
extra¶
Type: Dict[str, Any]
See extra
extra_evaluated¶
Type: Dict[str, Any]
See extra
extra_params¶
See extra_params
group (evaluated)¶
Type: str
- Cookbook:
help¶
Type: Fragment
help_text¶
The help text will be grabbed from the django model if specified and available.
include (evaluated)¶
Type: bool
See include
initial (evaluated)¶
Initial value of the field
Type: Any
input¶
Type: Fragment
iommi_style¶
Type: str
See iommi_style
is_boolean (evaluated)¶
Type: bool
Default: False
is_list (evaluated)¶
Interpret request data as a list (can NOT be a callable). Default: False
Type: bool
Default: False
is_valid¶
Validation function. Should return a tuple of (bool, reason_for_failure_if_bool_is_false) or raise ValidationError.
form = Form.create(
auto__model=Artist,
fields__name__is_valid=lambda parsed_data, **_: (parsed_data.startswith('H'), 'Must start with H!'),
)
label¶
Type: Fragment
model (evaluated)¶
Type: Optional[Type[Model]]
model_field¶
Type: Optional[Field]
model_field_name¶
non_editable_input¶
Type: Fragment
Default: {'call_target': <class 'iommi.fragment.Fragment'>, 'children__text': <function Field.<lambda> at 0x7eb20c691620>, 'attrs__value': <function Field.<lambda> at 0x7eb20c6916c0>}
parse¶
Parse function. Default just returns the string input unchanged. This function can raise ValueError or ValidationError to produce a field error message.
parse_empty_string_as_none (evaluated)¶
Type: bool
Default: True
parsed_data (evaluated)¶
Type: Any
- Cookbook:
post_validation¶
raw_data¶
Type: str
read_from_instance¶
Callback to retrieve value from edited instance. Invoked with parameters field and instance.
render_value¶
Render the parsed and validated value into a string. Default just converts to str.
sentinel = '!!custom!!'
form = Form(
fields__foo=Field(
initial='not sentinel value',
render_value=lambda form, field, value, **_: sentinel,
)
)
render_value_on_error¶
required (evaluated)¶
If the field is a required field. Default: True
Type: bool
Default: True
- Cookbook:
search_fields¶
strip_input (evaluated)¶
Runs the input data through standard python .strip() before passing it to the parse function (can NOT be callable). Default: True
Type: bool
Default: True
tag (evaluated)¶
Type: str
- Default:
div See tag
template (evaluated)¶
Django template filename or Template instance for the entire row. Normally you shouldn’t need to override on this level. Prefer overriding input__template, label__template or error__template as needed.
Type: Union[str, Template]
See template
write_to_instance¶
Callback to write value to instance. Invoked with parameters field, instance and value.
Shortcuts¶
Field.boolean¶
Defaults¶
parseiommi.form.bool_parse
requiredFalse
is_booleanTrue
Field.boolean_tristate¶
Parent: Field.choice
Defaults¶
choices[True, False]
choice_id_formatterlambda choice, **_: 'true' if choice else 'false'
choice_display_name_formatterlambda choice, **_: gettext_lazy('Yes') if choice else gettext_lazy('No')
parseiommi.form.boolean_tristate__parse
requiredFalse
Field.checkboxes¶
Parent: Field.multi_choice
Defaults¶
input__attrs__idNone
extra_evaluated__idiommi.form.default_input_id
Field.choice¶
Shortcut for single choice field. If required is false it will automatically add an option first with the value ‘’ and the title ‘—’. To override that text pass in the parameter empty_label.
Defaults¶
requiredTrue
is_listFalse
is_validiommi.form.choice_is_valid
input__attrs__multiplelambda field, **_: True if field.is_list else None
parseiommi.form.choice_parse
Field.choice_queryset¶
Parent: Field.choice
Defaults¶
parseiommi.form.choice_queryset__parse
choice_id_formatterlambda choice, **_: choice.pk
endpoints__choices__funciommi.form.choice_queryset__endpoint_handler
is_validiommi.form.choice_queryset__is_valid
extra__filter_and_sortiommi.form.choice_queryset__extra__filter_and_sort
extra__model_from_choicesiommi.form.choice_queryset__extra__model_from_choices
Field.choice_searchable¶
Shortcut for single choice field, but with select2.
Parent: Field.choice
Field.date¶
Defaults¶
parseiommi.form.date_parse
render_valueiommi.form.date_render_value
Field.datetime¶
Defaults¶
parseiommi.form.datetime_parse
render_valueiommi.form.datetime_render_value
extra_evaluated__is_tz_awarelambda **_: settings.USE_TZ
Field.decimal¶
Parent: Field.number
Defaults¶
parseiommi.form.decimal_parse
Field.dropfile¶
Parent: Field.file
Field.dropimage¶
Parent: Field.dropfile
Field.duration¶
Parent: Field.text
Defaults¶
parseiommi.form.duration_parse
render_valueiommi.form.duration_render_value
Field.email¶
Defaults¶
input__attrs__typeemail
parseiommi.form.email_parse
Field.file¶
Defaults¶
input__attrs__typefile
input__attrs__multiplelambda field, **_: True if field.is_list else None
raw_dataiommi.form.file__raw_data
write_to_instanceiommi.form.file_write_to_instance
extra__django_related_fieldTrue
extra_evaluated__show_thumbsTrue
attrs__data-iommi-extended-file-fieldTrue
attrs__data-iommi-extended-file-with-thumbslambda field, **_: field.extra_evaluated.show_thumbs
attrs__data-iommi-extended-file-thumb-width100
attrs__data-iommi-extended-file-thumb-height100
attrs__data-iommi-extended-file-loading-iconlambda **_: static('images/iommi-icons/spinner.svg')
Field.float¶
Parent: Field.number
Defaults¶
parseiommi.form.float_parse
Field.foreign_key¶
Field.foreign_key_reverse¶
Parent: Field.related_multiple
Defaults¶
editableFalse
display_namelambda field, **_: capitalize(field.model_field.related_model._meta.verbose_name_plural)
choicesiommi.form.related__choices
extra__django_related_fieldTrue
Field.hardcoded¶
Field.heading¶
Defaults¶
editableFalse
attrNone
Field.image¶
Parent: Field.file
Field.info¶
Shortcut to create an info entry.
Defaults¶
editableFalse
attrNone
Field.integer¶
Parent: Field.number
Defaults¶
parseiommi.form.int_parse
Field.many_to_many¶
Parent: Field.related_multiple
Field.many_to_many_reverse¶
Parent: Field.related_multiple
Field.multi_choice¶
Parent: Field.choice
Defaults¶
is_listTrue
Field.multi_choice_queryset¶
Parent: Field.choice_queryset
Defaults¶
is_listTrue
Field.non_rendered¶
Defaults¶
template<iommi._web_compat.Template object at 0x7eb20c770500>
editableFalse
Field.number¶
Field.password¶
Defaults¶
input__attrs__typepassword
render_valuelambda **_: ''
render_value_on_errorlambda **_: ''
Field.phone_number¶
Defaults¶
is_validiommi.form.phone_number_is_valid
Field.radio¶
Parent: Field.choice
Defaults¶
input__attrs__idNone
extra_evaluated__idiommi.form.default_input_id
Field.text¶
Defaults¶
input__attrs__typetext
Field.textarea¶
Defaults¶
input__tagtextarea
input__attrs__typeNone
input__attrs__valueNone
input__children__textlambda field, **_: field.rendered_value
Field.time¶
Defaults¶
parseiommi.form.time_parse
render_valueiommi.form.time_render_value
Field.url¶
Defaults¶
input__attrs__typeurl
parseiommi.form.url_parse