diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 81ae8f093a..a1537f0a94 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -337,7 +337,7 @@ class Field(object): def formfield(self): "Returns a django.newforms.Field instance for this database Field." # TODO: This is just a temporary default during development. - return forms.CharField(label=capfirst(self.verbose_name)) + return forms.CharField(required=not self.blank, label=capfirst(self.verbose_name)) class AutoField(Field): empty_strings_allowed = False @@ -389,6 +389,9 @@ class BooleanField(Field): def get_manipulator_field_objs(self): return [oldforms.CheckboxField] + def formfield(self): + return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name)) + class CharField(Field): def get_manipulator_field_objs(self): return [oldforms.TextField] @@ -403,6 +406,9 @@ class CharField(Field): raise validators.ValidationError, gettext_lazy("This field cannot be null.") return str(value) + def formfield(self): + return forms.CharField(max_length=self.maxlength, required=not self.blank, label=capfirst(self.verbose_name)) + # TODO: Maybe move this into contrib, because it's specialized. class CommaSeparatedIntegerField(CharField): def get_manipulator_field_objs(self): @@ -474,6 +480,9 @@ class DateField(Field): val = self._get_val_from_obj(obj) return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')} + def formfield(self): + return forms.DateField(required=not self.blank, label=capfirst(self.verbose_name)) + class DateTimeField(DateField): def to_python(self, value): if isinstance(value, datetime.datetime): @@ -532,6 +541,9 @@ class DateTimeField(DateField): return {date_field: (val is not None and val.strftime("%Y-%m-%d") or ''), time_field: (val is not None and val.strftime("%H:%M:%S") or '')} + def formfield(self): + return forms.DateTimeField(required=not self.blank, label=capfirst(self.verbose_name)) + class EmailField(CharField): def __init__(self, *args, **kwargs): kwargs['maxlength'] = 75 @@ -546,6 +558,9 @@ class EmailField(CharField): def validate(self, field_data, all_data): validators.isValidEmail(field_data, all_data) + def formfield(self): + return forms.EmailField(required=not self.blank, label=capfirst(self.verbose_name)) + class FileField(Field): def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs): self.upload_to = upload_to @@ -678,6 +693,9 @@ class IntegerField(Field): def get_manipulator_field_objs(self): return [oldforms.IntegerField] + def formfield(self): + return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name)) + class IPAddressField(Field): def __init__(self, *args, **kwargs): kwargs['maxlength'] = 15 @@ -772,15 +790,22 @@ class TimeField(Field): val = self._get_val_from_obj(obj) return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')} + def formfield(self): + return forms.TimeField(required=not self.blank, label=capfirst(self.verbose_name)) + class URLField(Field): def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): if verify_exists: kwargs.setdefault('validator_list', []).append(validators.isExistingURL) + self.verify_exists = verify_exists Field.__init__(self, verbose_name, name, **kwargs) def get_manipulator_field_objs(self): return [oldforms.URLField] + def formfield(self): + return forms.URLField(required=not self.blank, verify_exists=self.verify_exists, label=capfirst(self.verbose_name)) + class USStateField(Field): def get_manipulator_field_objs(self): return [oldforms.USStateField] diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index b51b4e1a8b..2f6cf340d4 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -8,7 +8,7 @@ from django.db import models class Category(models.Model): name = models.CharField(maxlength=20) - url = models.CharField('The URL', maxlength=20) + url = models.CharField('The URL', maxlength=40) def __str__(self): return self.name @@ -27,18 +27,18 @@ __test__ = {'API_TESTS': """ >>> f = CategoryForm() >>> print f - - + + >>> print f.as_ul()
  • -
  • -
  • +
  • +
  • >>> print f['name'] - + >>> f = CategoryForm(auto_id=False) >>> print f.as_ul()
  • ID:
  • -
  • Name:
  • -
  • The URL:
  • +
  • Name:
  • +
  • The URL:
  • """}