MCPcopy
hub / github.com/django/django / CharField

Class CharField

django/forms/fields.py:276–309  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

274
275
276class CharField(Field):
277 def __init__(
278 self, *, max_length=None, min_length=None, strip=True, empty_value="", **kwargs
279 ):
280 self.max_length = max_length
281 self.min_length = min_length
282 self.strip = strip
283 self.empty_value = empty_value
284 super().__init__(**kwargs)
285 if min_length is not None:
286 self.validators.append(validators.MinLengthValidator(int(min_length)))
287 if max_length is not None:
288 self.validators.append(validators.MaxLengthValidator(int(max_length)))
289 self.validators.append(validators.ProhibitNullCharactersValidator())
290
291 def to_python(self, value):
292 """Return a string."""
293 if value not in self.empty_values:
294 value = str(value)
295 if self.strip:
296 value = value.strip()
297 if value in self.empty_values:
298 return self.empty_value
299 return value
300
301 def widget_attrs(self, widget):
302 attrs = super().widget_attrs(widget)
303 if self.max_length is not None and not widget.is_hidden:
304 # The HTML attribute is maxlength, not max_length.
305 attrs["maxlength"] = str(self.max_length)
306 if self.min_length is not None and not widget.is_hidden:
307 # The HTML attribute is minlength, not min_length.
308 attrs["minlength"] = str(self.min_length)
309 return attrs
310
311
312class IntegerField(Field):

Callers 15

TestFormClass · 0.90
test_combofield_1Method · 0.90
test_combofield_2Method · 0.90
JSONFormClass · 0.90
__init__Method · 0.90
test_charfield_1Method · 0.90
test_charfield_2Method · 0.90
test_charfield_3Method · 0.90
test_charfield_4Method · 0.90
test_charfield_5Method · 0.90

Calls

no outgoing calls

Tested by 15

test_combofield_1Method · 0.72
test_combofield_2Method · 0.72
__init__Method · 0.72
test_charfield_1Method · 0.72
test_charfield_2Method · 0.72
test_charfield_3Method · 0.72
test_charfield_4Method · 0.72
test_charfield_5Method · 0.72
test_charfield_stripMethod · 0.72