我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用django.forms.fields.CharField()。
def __init__(self, prop, name): self.prop = prop self.name = name self.help_text = getattr(prop, 'help_text', '') self.primary_key = getattr(prop, 'primary_key', False) self.label = prop.label if prop.label else name form_cls = getattr(prop, 'form_field_class', 'Field') # get field string self.form_class = getattr(fields, form_cls, fields.CharField) self._has_default = prop.has_default self.required = prop.required self.blank = not self.required self.choices = getattr(prop, 'choices', None) self.creation_counter = DjangoField.creation_counter DjangoField.creation_counter += 1
def __init__(self, fields=(), keys=(), require_all_fields=False, max_value_length=25, *args, **kwargs): if (fields and keys) or (not fields and not keys): raise ValueError("NestedFormField requires either a tuple of fields or keys but not both") if keys: fields = [] for key in keys: field = CharField(max_length=max_value_length, required=False) fields.append((key, field)) form_fields = [] widgets = [] self.labels = [] self.names = {} for field in fields: label = field[1].label or field[0] self.names[label] = field[0] self.labels.append(label) form_fields.append(field[1]) widgets.append(field[1].widget) widget = NestedFormWidget(self.labels, widgets, self.names) super(NestedFormField, self).__init__(*args, fields=form_fields, widget=widget, require_all_fields=require_all_fields, **kwargs)
def test_subclass_field(self): assert issubclass(PopupViewField, CharField) is True
def test_username_field_label(self): class CustomAuthenticationForm(AuthenticationForm): username = CharField(label="Name", max_length=75) form = CustomAuthenticationForm() self.assertEqual(form['username'].label, "Name")
def test_username_field_label_not_set(self): class CustomAuthenticationForm(AuthenticationForm): username = CharField() form = CustomAuthenticationForm() UserModel = get_user_model() username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD) self.assertEqual(form.fields['username'].label, capfirst(username_field.verbose_name))
def test_username_field_label_empty_string(self): class CustomAuthenticationForm(AuthenticationForm): username = CharField(label='') form = CustomAuthenticationForm() self.assertEqual(form.fields['username'].label, "")
def assertFieldOutput(self, fieldclass, valid, invalid, field_args=None, field_kwargs=None, empty_value=''): """ Asserts that a form field behaves correctly with various inputs. Args: fieldclass: the class of the field to be tested. valid: a dictionary mapping valid inputs to their expected cleaned values. invalid: a dictionary mapping invalid inputs to one or more raised error messages. field_args: the args passed to instantiate the field field_kwargs: the kwargs passed to instantiate the field empty_value: the expected clean output for inputs in empty_values """ if field_args is None: field_args = [] if field_kwargs is None: field_kwargs = {} required = fieldclass(*field_args, **field_kwargs) optional = fieldclass(*field_args, **dict(field_kwargs, required=False)) # test valid inputs for input, output in valid.items(): self.assertEqual(required.clean(input), output) self.assertEqual(optional.clean(input), output) # test invalid inputs for input, errors in invalid.items(): with self.assertRaises(ValidationError) as context_manager: required.clean(input) self.assertEqual(context_manager.exception.messages, errors) with self.assertRaises(ValidationError) as context_manager: optional.clean(input) self.assertEqual(context_manager.exception.messages, errors) # test required inputs error_required = [force_text(required.error_messages['required'])] for e in required.empty_values: with self.assertRaises(ValidationError) as context_manager: required.clean(e) self.assertEqual(context_manager.exception.messages, error_required) self.assertEqual(optional.clean(e), empty_value) # test that max_length and min_length are always accepted if issubclass(fieldclass, CharField): field_kwargs.update({'min_length': 2, 'max_length': 20}) self.assertIsInstance(fieldclass(*field_args, **field_kwargs), fieldclass)