pytoolbox.django.forms.utils module

Some utilities related to the forms.

pytoolbox.django.forms.utils.conditional_required(form, required_dict, data=None, cleanup=False)[source]

Toggle requirement of some fields based on a dictionary with ‘field name’ -> ‘required boolean’.

pytoolbox.django.forms.utils.get_instance(form, field_name, request, msg=None)[source]

Return the instance if the form is valid, or try to get it from database. Return None if not found and add an error message if set.

pytoolbox.django.forms.utils.set_disabled(form, field_name, value=False)[source]

Toggle the disabled attribute of a form’s field.

pytoolbox.django.forms.utils.update_widget_attributes(widget, updates)[source]

Update attributes of a widget with content of updates handling classes addition [+], removal [-] and toggle [^].

Example usage

>>> from pytoolbox.unittest import asserts
>>> widget = type(str(''), (), {})
>>> widget.attrs = {'class': 'mondiale'}
>>> update_widget_attributes(
...     widget, {'class': '+pigeon +pigeon +voyage -mondiale -mondiale, ^voyage ^voyageur'})
>>> asserts.dict_equal(widget.attrs, {'class': 'pigeon voyageur'})
>>> update_widget_attributes(widget, {'class': '+le', 'cols': 100})
>>> asserts.dict_equal(widget.attrs, {'class': 'le pigeon voyageur', 'cols': 100})
pytoolbox.django.forms.utils.validate_start_end(form, data=None, start_name='start_date', end_name='end_date')[source]

Check that the field containing the value of the start field (time, …) is not bigger (>) than the stop.