Changed database Field formfield() methods to take arbitrary kwargs, which are passed directly into the given Field constructor

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4389 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-01-22 06:32:14 +00:00
parent af377b13e1
commit 2e042f33e3
2 changed files with 55 additions and 30 deletions

View File

@ -334,10 +334,11 @@ class Field(object):
return self._choices return self._choices
choices = property(_get_choices) choices = property(_get_choices)
def formfield(self, initial=None): def formfield(self, **kwargs):
"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. defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
return forms.CharField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults.update(kwargs)
return forms.CharField(**defaults)
def value_from_object(self, obj): def value_from_object(self, obj):
"Returns the value of this field in the given model instance." "Returns the value of this field in the given model instance."
@ -379,7 +380,7 @@ class AutoField(Field):
super(AutoField, self).contribute_to_class(cls, name) super(AutoField, self).contribute_to_class(cls, name)
cls._meta.has_auto_field = True cls._meta.has_auto_field = True
def formfield(self, initial=None): def formfield(self, **kwargs):
return None return None
class BooleanField(Field): class BooleanField(Field):
@ -396,8 +397,10 @@ class BooleanField(Field):
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [oldforms.CheckboxField] return [oldforms.CheckboxField]
def formfield(self, initial=None): def formfield(self, **kwargs):
return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.BooleanField(**defaults)
class CharField(Field): class CharField(Field):
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
@ -413,8 +416,10 @@ 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, initial=None): def formfield(self, **kwargs):
return forms.CharField(max_length=self.maxlength, required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.CharField(**defaults)
# 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):
@ -487,8 +492,10 @@ 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, initial=None): def formfield(self, **kwargs):
return forms.DateField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.DateField(**defaults)
class DateTimeField(DateField): class DateTimeField(DateField):
def to_python(self, value): def to_python(self, value):
@ -548,8 +555,10 @@ 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, initial=None): def formfield(self, **kwargs):
return forms.DateTimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.DateTimeField(**defaults)
class EmailField(CharField): class EmailField(CharField):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -565,8 +574,10 @@ 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, initial=None): def formfield(self, **kwargs):
return forms.EmailField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.EmailField(**defaults)
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):
@ -700,8 +711,10 @@ class IntegerField(Field):
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [oldforms.IntegerField] return [oldforms.IntegerField]
def formfield(self, initial=None): def formfield(self, **kwargs):
return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.IntegerField(**defaults)
class IPAddressField(Field): class IPAddressField(Field):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -757,8 +770,10 @@ class TextField(Field):
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [oldforms.LargeTextField] return [oldforms.LargeTextField]
def formfield(self, initial=None): def formfield(self, **kwargs):
return forms.CharField(required=not self.blank, widget=forms.Textarea, label=capfirst(self.verbose_name), initial=initial) defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.CharField(**defaults)
class TimeField(Field): class TimeField(Field):
empty_strings_allowed = False empty_strings_allowed = False
@ -800,8 +815,10 @@ 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, initial=None): def formfield(self, **kwargs):
return forms.TimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.TimeField(**defaults)
class URLField(CharField): class URLField(CharField):
def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
@ -817,8 +834,10 @@ class URLField(CharField):
def get_internal_type(self): def get_internal_type(self):
return "CharField" return "CharField"
def formfield(self, initial=None): def formfield(self, **kwargs):
return forms.URLField(required=not self.blank, verify_exists=self.verify_exists, label=capfirst(self.verbose_name), initial=initial) defaults = {'required': not self.blank, 'verify_exists': self.verify_exists, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.URLField(**defaults)
class USStateField(Field): class USStateField(Field):
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):

View File

@ -548,8 +548,10 @@ class ForeignKey(RelatedField, Field):
def contribute_to_related_class(self, cls, related): def contribute_to_related_class(self, cls, related):
setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related)) setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
def formfield(self, initial=None): def formfield(self, **kwargs):
return forms.ChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults = {'choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.ChoiceField(**defaults)
class OneToOneField(RelatedField, IntegerField): class OneToOneField(RelatedField, IntegerField):
def __init__(self, to, to_field=None, **kwargs): def __init__(self, to, to_field=None, **kwargs):
@ -612,8 +614,10 @@ class OneToOneField(RelatedField, IntegerField):
if not cls._meta.one_to_one_field: if not cls._meta.one_to_one_field:
cls._meta.one_to_one_field = self cls._meta.one_to_one_field = self
def formfield(self, initial=None): def formfield(self, **kwargs):
return forms.ChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults = {'choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.ChoiceField(**kwargs)
class ManyToManyField(RelatedField, Field): class ManyToManyField(RelatedField, Field):
def __init__(self, to, **kwargs): def __init__(self, to, **kwargs):
@ -725,12 +729,14 @@ class ManyToManyField(RelatedField, Field):
"Returns the value of this field in the given model instance." "Returns the value of this field in the given model instance."
return getattr(obj, self.attname).all() return getattr(obj, self.attname).all()
def formfield(self, initial=None): def formfield(self, **kwargs):
# If initial is passed in, it's a list of related objects, but the # If initial is passed in, it's a list of related objects, but the
# MultipleChoiceField takes a list of IDs. # MultipleChoiceField takes a list of IDs.
if initial is not None: if kwargs.get('initial') is not None:
initial = [i._get_pk_val() for i in initial] kwargs['initial'] = [i._get_pk_val() for i in kwargs['initial']]
return forms.MultipleChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial) defaults = {'choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name)}
defaults.update(kwargs)
return forms.MultipleChoiceField(**defaults)
class ManyToOneRel(object): class ManyToOneRel(object):
def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None, def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None,