def get_bound_field(self, form, field_name): bound_field = BoundField(form, self, field_name) # Override initial() to allow passing multiple values bound_field.initial = self._get_initial_value(form.initial, 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 = bound_field.value() 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'): app_label = self.queryset.model._meta.app_label model_name = self.queryset.model._meta.model_name data_url = reverse('{}-api:{}-list'.format(app_label, model_name)) widget.attrs['data-url'] = data_url return bound_field
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 = bound_field.value() if data: field_name = getattr(self, 'to_field_name') or 'pk' filter = self.filter(field_name=field_name) try: self.queryset = filter.filter(self.queryset, data) except TypeError: # Catch any error caused by invalid initial data passed from the user self.queryset = self.queryset.none() 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'): app_label = self.queryset.model._meta.app_label model_name = self.queryset.model._meta.model_name data_url = reverse('{}-api:{}-list'.format(app_label, model_name)) widget.attrs['data-url'] = data_url return bound_field
def test_get_field_options_value(self): unbound_field = ChoiceField() field = BoundField(Form(initial={"field": "Value"}), unbound_field, "field") context = Context({"field": field}) self.TEMPLATE.render(context)
def _convert_to_bound_fields(form, i18n_field_names): bound_fields = [] for field_name in i18n_field_names: local_fields = field_mapping[field_name] for local_name in local_fields: local_field = form_instance.fields[local_name] bound_field = BoundField(form, local_field, local_name) bound_fields.append(bound_field) return bound_fields
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() return bound_field
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. field_name = '{}{}'.format(self.to_field_name or 'pk', self.field_modifier) if bound_field.data: self.queryset = self.queryset.filter(**{field_name: self.prepare_value(bound_field.data)}) elif bound_field.initial: self.queryset = self.queryset.filter(**{field_name: self.prepare_value(bound_field.initial)}) else: self.queryset = self.queryset.none() return bound_field
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
def get_bound_field(self, form, field_name): bound_field = BoundField(form, self, field_name) # Set the initial value based on prescribed child fields (if not set) if not self.initial and self.initial_params: filter_kwargs = {} for kwarg, child_field in self.initial_params.items(): value = form.initial.get(child_field.lstrip("$")) if value: filter_kwargs[kwarg] = value if filter_kwargs: self.initial = self.queryset.filter(**filter_kwargs).first() # 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 = bound_field.value() if data: field_name = getattr(self, "to_field_name") or "pk" filter = self.filter(field_name=field_name) try: self.queryset = filter.filter(self.queryset, data) except (TypeError, ValueError): # Catch errors caused by invalid initial data self.queryset = self.queryset.none() 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
def get_bound_field(self, form, field_name): bound_field = BoundField(form, self, field_name) # Set initial value based on prescribed child fields (if not already set) if not self.initial and self.initial_params: filter_kwargs = {} for kwarg, child_field in self.initial_params.items(): value = form.initial.get(child_field.lstrip('$')) if value: filter_kwargs[kwarg] = value if filter_kwargs: self.initial = self.queryset.filter(**filter_kwargs).first() # 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 = bound_field.value() if data: field_name = getattr(self, 'to_field_name') or 'pk' filter = self.filter(field_name=field_name) try: self.queryset = filter.filter(self.queryset, data) except TypeError: # Catch any error caused by invalid initial data passed from the user self.queryset = self.queryset.none() 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'): app_label = self.queryset.model._meta.app_label model_name = self.queryset.model._meta.model_name data_url = reverse('{}-api:{}-list'.format(app_label, model_name)) widget.attrs['data-url'] = data_url return bound_field
def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row): "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()." top_errors = self.non_field_errors( ) # Errors that should be displayed above all fields. output, hidden_fields = [], [] for fieldset, fields in self.fieldsets: if fieldset: output.append( normal_row % { 'errors': '', 'label': ' ', 'field': self.fieldset_template % fieldset, 'help_text': '' }) for name, field in [ i for i in self.fields.items() if i[0] in fields ]: bf = BoundField(self, field, name) bf_errors = self.error_class([ escape(error) for error in bf.errors ]) # Escape and cache in local variable. if bf.is_hidden: if bf_errors: top_errors.extend([ u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors ]) hidden_fields.append(unicode(bf)) else: if errors_on_separate_row and bf_errors: output.append(error_row % force_unicode(bf_errors)) if bf.label: label = escape(force_unicode(bf.label)) # Only add the suffix if the label does not end in # punctuation. if self.label_suffix: if label[-1] not in ':?.!': label += self.label_suffix label = bf.label_tag(label) or '' else: label = '' if field.help_text: help_text = help_text_html % force_unicode( field.help_text) else: help_text = u'' output.append( normal_row % { 'errors': force_unicode(bf_errors), 'label': force_unicode(label), 'field': unicode(bf), 'help_text': help_text }) if top_errors: output.insert(0, error_row % force_unicode(top_errors)) if hidden_fields: # Insert any hidden fields in the last row. str_hidden = u''.join(hidden_fields) if output: last_row = output[-1] # Chop off the trailing row_ender (e.g. '</td></tr>') and # insert the hidden fields. output[ -1] = last_row[:-len(row_ender)] + str_hidden + row_ender else: # If there aren't any rows in the output, just append the # hidden fields. output.append(str_hidden) return mark_safe(u'\n'.join(output))
def test_bound_field_labels_not_boolean(self): unbound_field = Field() field = BoundField(Form(), unbound_field, "field") context = Context({"field": field}) self.TEMPLATE_LABELS_NOT_BOOLEAN.render(context)
def test_bound_field_no_labels(self): unbound_field = Field() field = BoundField(Form(), unbound_field, "field") context = Context({"field": field}) self.TEMPLATE_NO_LABELS.render(context)
def test_bound_field(self): unbound_field = Field() field = BoundField(Form(), unbound_field, "field") context = Context({"field": field}) self.TEMPLATE.render(context)
def test_get_field_options(self): unbound_field = ChoiceField() field = BoundField(Form(), unbound_field, "field") context = Context({"field": field}) self.TEMPLATE.render(context)