Python django.forms.BoundField() Examples

The following are 12 code examples for showing how to use django.forms.BoundField(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.forms , or try the search function .

Example 1
Project: peering-manager   Author: respawner   File: forms.py    License: Apache License 2.0 6 votes vote down vote up
def get_bound_field(self, form, field_name):
        bound_field = BoundField(form, self, field_name)

        # Modify the QuerySet of the field before we return it. Limit choices to any
        # data already bound: Options will be populated on-demand via the APISelect
        # widget
        data = self.prepare_value(bound_field.data or bound_field.initial)
        if data:
            filter = self.filter(
                field_name=self.to_field_name or "pk", queryset=self.queryset
            )
            self.queryset = filter.filter(self.queryset, data)
        else:
            self.queryset = self.queryset.none()

        # Set the data URL on the APISelect widget (if not already set)
        widget = bound_field.field.widget
        if not widget.attrs.get("data-url"):
            data_url = reverse(
                f"{self.queryset.model._meta.app_label}-api:{self.queryset.model._meta.model_name}-list"
            )
            widget.attrs["data-url"] = data_url

        return bound_field 
Example 2
Project: django-osm-field   Author: MarkusH   File: forms.py    License: MIT License 5 votes vote down vote up
def get_bound_field(self, form, field_name):
        """
        For Django 1.9+

        Return a BoundField instance that will be used when accessing the form
        field in a template.
        """
        return PrefixedBoundField(form, self, field_name) 
Example 3
Project: cadasta-platform   Author: Cadasta   File: test_filters.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_display_choice_verbose(self):
        form = MockForm(data=self.data)
        field = form.fields.get('type')
        bf = forms.BoundField(form, field, 'type')
        value = filters.display_choice_verbose(bf)
        assert value == 'Group' 
Example 4
Project: cadasta-platform   Author: Cadasta   File: test_filters.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_field_value(self):
        form = MockForm(data=self.data)
        field = form.fields.get('type')
        bf = forms.BoundField(form, field, 'type')
        value = filters.field_value(bf)
        assert value == 'GR' 
Example 5
Project: cadasta-platform   Author: Cadasta   File: test_filters.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_field_value_file_field(self):
        form = MockForm(
            data=self.data,
            files={'file': SimpleUploadedFile('test.txt', b'some content')}
        )
        field = form.fields.get('file')
        bf = forms.BoundField(form, field, 'file')
        value = filters.field_value(bf)
        assert value.name == 'test.txt' 
Example 6
Project: cadasta-platform   Author: Cadasta   File: test_filters.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_blank_file_field(self):
        form = MockForm(data=self.data)
        field = form.fields.get('file')
        bf = forms.BoundField(form, field, 'file')
        value = filters.field_value(bf)
        assert value == '' 
Example 7
Project: cadasta-platform   Author: Cadasta   File: test_filters.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_field_value_as_callable(self):
        form = MockForm()
        field = form.fields.get('date')
        bf = forms.BoundField(form, field, 'date')
        value = filters.field_value(bf)
        assert isinstance(value, datetime.datetime) 
Example 8
Project: cadasta-platform   Author: Cadasta   File: test_filters.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_set_parsley_sanitize(self):
        form = MockForm(data={'name', 'Test'})
        field = form.fields.get('name')
        bf = forms.BoundField(form, field, 'name')
        filters.set_parsley_sanitize(bf)
        assert bf.field.widget.attrs == {'data-parsley-sanitize': '1'} 
Example 9
Project: site   Author: python-discord   File: wiki_extra.py    License: MIT License 5 votes vote down vote up
def get_unbound_field(field: Union[BoundField, Field]) -> Field:
    """
    Unwraps a bound Django Forms field, returning the unbound field.

    Bound fields often don't give you the same level of access to the field's underlying attributes,
    so sometimes it helps to have access to the underlying field object.
    """
    while isinstance(field, BoundField):
        field = field.field

    return field 
Example 10
Project: site   Author: python-discord   File: wiki_extra.py    License: MIT License 5 votes vote down vote up
def get_field_options(context: Context, field: BoundField) -> str:
    """
    Retrieves the field options for a multiple choice field, and stores it in the context.

    This tag exists because we can't call functions within Django templates directly, and is
    only made use of in the template for ModelChoice (and derived) fields - but would work fine
    with anything that makes use of your standard `<select>` element widgets.

    This stores the parsed options under `options` in the context, which will subsequently
    be available in the template.

    Usage:

    ```django
    {% get_field_options field_object %}

    {% if options %}
      {% for group_name, group_choices, group_index in options %}
        ...
      {% endfor %}
    {% endif %}
    ```
    """
    widget = field.field.widget

    if field.value() is None:
        value: List[str] = []
    else:
        value = [str(field.value())]

    context["options"] = widget.optgroups(field.name, value)
    return "" 
Example 11
Project: colossus   Author: vitorfs   File: forms.py    License: MIT License 5 votes vote down vote up
def column_mapping_fields(self) -> List[BoundField]:
        """
        Convenience method to be used in the front-end to render import template
        table.

        :return: List of BoundField objects that are visible and part of the
                 columns mapping fields.
        """
        return [field for field in self.visible_fields() if field.name.startswith('__column_')] 
Example 12
Project: colossus   Author: vitorfs   File: forms.py    License: MIT License 5 votes vote down vote up
def import_settings_fields(self) -> List[BoundField]:
        """
        Convenience method to be used in the front-end to list visible fields
        not related to columns mapping.

        :return: List of BoundField objects that are visible and not part of the
                 columns mapping fields.
        """
        return [field for field in self.visible_fields() if not field.name.startswith('__column_')]