Implemented formfield() for a bunch of database Field classes

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4211 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-12-15 18:32:42 +00:00
parent 1c4ed6126b
commit 35f7e33b4a
2 changed files with 34 additions and 9 deletions

View File

@ -337,7 +337,7 @@ class Field(object):
def formfield(self): def formfield(self):
"Returns a django.newforms.Field instance for this database Field." "Returns a django.newforms.Field instance for this database Field."
# TODO: This is just a temporary default during development. # 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): class AutoField(Field):
empty_strings_allowed = False empty_strings_allowed = False
@ -389,6 +389,9 @@ class BooleanField(Field):
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [oldforms.CheckboxField] return [oldforms.CheckboxField]
def formfield(self):
return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name))
class CharField(Field): class CharField(Field):
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [oldforms.TextField] return [oldforms.TextField]
@ -403,6 +406,9 @@ class CharField(Field):
raise validators.ValidationError, gettext_lazy("This field cannot be null.") raise validators.ValidationError, gettext_lazy("This field cannot be null.")
return str(value) 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. # TODO: Maybe move this into contrib, because it's specialized.
class CommaSeparatedIntegerField(CharField): class CommaSeparatedIntegerField(CharField):
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
@ -474,6 +480,9 @@ class DateField(Field):
val = self._get_val_from_obj(obj) val = self._get_val_from_obj(obj)
return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')} 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): class DateTimeField(DateField):
def to_python(self, value): def to_python(self, value):
if isinstance(value, datetime.datetime): 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 ''), 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 '')} 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): class EmailField(CharField):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
kwargs['maxlength'] = 75 kwargs['maxlength'] = 75
@ -546,6 +558,9 @@ class EmailField(CharField):
def validate(self, field_data, all_data): def validate(self, field_data, all_data):
validators.isValidEmail(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): class FileField(Field):
def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs): def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
self.upload_to = upload_to self.upload_to = upload_to
@ -678,6 +693,9 @@ class IntegerField(Field):
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [oldforms.IntegerField] return [oldforms.IntegerField]
def formfield(self):
return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name))
class IPAddressField(Field): class IPAddressField(Field):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
kwargs['maxlength'] = 15 kwargs['maxlength'] = 15
@ -772,15 +790,22 @@ class TimeField(Field):
val = self._get_val_from_obj(obj) val = self._get_val_from_obj(obj)
return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')} 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): class URLField(Field):
def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
if verify_exists: if verify_exists:
kwargs.setdefault('validator_list', []).append(validators.isExistingURL) kwargs.setdefault('validator_list', []).append(validators.isExistingURL)
self.verify_exists = verify_exists
Field.__init__(self, verbose_name, name, **kwargs) Field.__init__(self, verbose_name, name, **kwargs)
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [oldforms.URLField] 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): class USStateField(Field):
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [oldforms.USStateField] return [oldforms.USStateField]

View File

@ -8,7 +8,7 @@ from django.db import models
class Category(models.Model): class Category(models.Model):
name = models.CharField(maxlength=20) name = models.CharField(maxlength=20)
url = models.CharField('The URL', maxlength=20) url = models.CharField('The URL', maxlength=40)
def __str__(self): def __str__(self):
return self.name return self.name
@ -27,18 +27,18 @@ __test__ = {'API_TESTS': """
>>> f = CategoryForm() >>> f = CategoryForm()
>>> print f >>> print f
<tr><th><label for="id_id">ID:</label></th><td><input type="text" name="id" id="id_id" /></td></tr> <tr><th><label for="id_id">ID:</label></th><td><input type="text" name="id" id="id_id" /></td></tr>
<tr><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name" /></td></tr> <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr>
<tr><th><label for="id_url">The URL:</label></th><td><input type="text" name="url" id="id_url" /></td></tr> <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
>>> print f.as_ul() >>> print f.as_ul()
<li><label for="id_id">ID:</label> <input type="text" name="id" id="id_id" /></li> <li><label for="id_id">ID:</label> <input type="text" name="id" id="id_id" /></li>
<li><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li> <li><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" maxlength="20" /></li>
<li><label for="id_url">The URL:</label> <input type="text" name="url" id="id_url" /></li> <li><label for="id_url">The URL:</label> <input id="id_url" type="text" name="url" maxlength="40" /></li>
>>> print f['name'] >>> print f['name']
<input type="text" name="name" id="id_name" /> <input id="id_name" type="text" name="name" maxlength="20" />
>>> f = CategoryForm(auto_id=False) >>> f = CategoryForm(auto_id=False)
>>> print f.as_ul() >>> print f.as_ul()
<li>ID: <input type="text" name="id" /></li> <li>ID: <input type="text" name="id" /></li>
<li>Name: <input type="text" name="name" /></li> <li>Name: <input type="text" name="name" maxlength="20" /></li>
<li>The URL: <input type="text" name="url" /></li> <li>The URL: <input type="text" name="url" maxlength="40" /></li>
"""} """}