Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored the formfield() methods on models.fields.Field subclasses …

…slightly

to remove some repetition.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5118 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b09bdd2ae8177325f414cf4ec116e8c8e17cd4c0 1 parent c675ee7
Malcolm Tredinnick malcolmt authored
44 django/db/models/fields/__init__.py
View
@@ -341,11 +341,11 @@ def _get_choices(self):
return self._choices
choices = property(_get_choices)
- def formfield(self, **kwargs):
+ def formfield(self, form_class=forms.CharField, **kwargs):
"Returns a django.newforms.Field instance for this database Field."
defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
defaults.update(kwargs)
- return forms.CharField(**defaults)
+ return form_class(**defaults)
def value_from_object(self, obj):
"Returns the value of this field in the given model instance."
@@ -405,9 +405,9 @@ def get_manipulator_field_objs(self):
return [oldforms.CheckboxField]
def formfield(self, **kwargs):
- defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'form_class': forms.BooleanField}
defaults.update(kwargs)
- return forms.BooleanField(**defaults)
+ return super(BooleanField, self).formfield(**defaults)
class CharField(Field):
def get_manipulator_field_objs(self):
@@ -424,9 +424,9 @@ def to_python(self, value):
return str(value)
def formfield(self, **kwargs):
- defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'max_length': self.maxlength}
defaults.update(kwargs)
- return forms.CharField(**defaults)
+ return super(CharField, self).formfield(**defaults)
# TODO: Maybe move this into contrib, because it's specialized.
class CommaSeparatedIntegerField(CharField):
@@ -502,9 +502,9 @@ def flatten_data(self, follow, obj=None):
return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')}
def formfield(self, **kwargs):
- defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'form_class': forms.DateField}
defaults.update(kwargs)
- return forms.DateField(**defaults)
+ return super(DateField, self).formfield(**defaults)
class DateTimeField(DateField):
def to_python(self, value):
@@ -567,9 +567,9 @@ def flatten_data(self,follow, obj = None):
time_field: (val is not None and val.strftime("%H:%M:%S") or '')}
def formfield(self, **kwargs):
- defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'form_class': forms.DateTimeField}
defaults.update(kwargs)
- return forms.DateTimeField(**defaults)
+ return super(DateTimeField, self).formfield(**defaults)
class EmailField(CharField):
def __init__(self, *args, **kwargs):
@@ -586,9 +586,9 @@ def validate(self, field_data, all_data):
validators.isValidEmail(field_data, all_data)
def formfield(self, **kwargs):
- defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'form_class': forms.EmailField}
defaults.update(kwargs)
- return forms.EmailField(**defaults)
+ return super(EmailField, self).formfield(**defaults)
class FileField(Field):
def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
@@ -723,9 +723,9 @@ def get_manipulator_field_objs(self):
return [oldforms.IntegerField]
def formfield(self, **kwargs):
- defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'form_class': forms.IntegerField}
defaults.update(kwargs)
- return forms.IntegerField(**defaults)
+ return super(IntegerField, self).formfield(**defaults)
class IPAddressField(Field):
def __init__(self, *args, **kwargs):
@@ -762,9 +762,9 @@ def validate(self, field_data, all_data):
def formfield(self, **kwargs):
from django.contrib.localflavor.us.forms import USPhoneNumberField
- defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'form_class': USPhoneNumberField}
defaults.update(kwargs)
- return USPhoneNumberField(**defaults)
+ return super(PhoneNumberField, self).formfield(**defaults)
class PositiveIntegerField(IntegerField):
def get_manipulator_field_objs(self):
@@ -795,9 +795,9 @@ def get_manipulator_field_objs(self):
return [oldforms.LargeTextField]
def formfield(self, **kwargs):
- defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'widget': forms.Textarea}
defaults.update(kwargs)
- return forms.CharField(**defaults)
+ return super(TextField, self).formfield(**defaults)
class TimeField(Field):
empty_strings_allowed = False
@@ -840,9 +840,9 @@ def flatten_data(self,follow, obj = None):
return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')}
def formfield(self, **kwargs):
- defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'form_class': forms.TimeField}
defaults.update(kwargs)
- return forms.TimeField(**defaults)
+ return super(TimeField, self).formfield(**defaults)
class URLField(CharField):
def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
@@ -859,9 +859,9 @@ def get_internal_type(self):
return "CharField"
def formfield(self, **kwargs):
- defaults = {'required': not self.blank, 'verify_exists': self.verify_exists, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'form_class': forms.URLField, 'verify_exists': self.verify_exists}
defaults.update(kwargs)
- return forms.URLField(**defaults)
+ return super(URLField, self).formfield(**defaults)
class USStateField(Field):
def get_manipulator_field_objs(self):
18 django/db/models/fields/related.py
View
@@ -553,9 +553,9 @@ def contribute_to_related_class(self, cls, related):
setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
def formfield(self, **kwargs):
- defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'form_class': forms.ModelChoiceField, 'queryset': self.rel.to._default_manager.all()}
defaults.update(kwargs)
- return forms.ModelChoiceField(**defaults)
+ return super(ForeignKey, self).formfield(**defaults)
class OneToOneField(RelatedField, IntegerField):
def __init__(self, to, to_field=None, **kwargs):
@@ -619,9 +619,9 @@ def contribute_to_related_class(self, cls, related):
cls._meta.one_to_one_field = self
def formfield(self, **kwargs):
- defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults = {'form_class': forms.ModelChoiceField, 'queryset': self.rel.to._default_manager.all()}
defaults.update(kwargs)
- return forms.ModelChoiceField(**defaults)
+ return super(OneToOneField, self).formfield(**defaults)
class ManyToManyField(RelatedField, Field):
def __init__(self, to, **kwargs):
@@ -738,13 +738,13 @@ def value_from_object(self, obj):
return getattr(obj, self.attname).all()
def formfield(self, **kwargs):
+ defaults = {'form_class': forms.ModelMultipleChoiceField, 'queryset': self.rel.to._default_manager.all()}
+ defaults.update(kwargs)
# If initial is passed in, it's a list of related objects, but the
# MultipleChoiceField takes a list of IDs.
- if kwargs.get('initial') is not None:
- kwargs['initial'] = [i._get_pk_val() for i in kwargs['initial']]
- defaults = {'queryset' : self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
- defaults.update(kwargs)
- return forms.ModelMultipleChoiceField(**defaults)
+ if defaults.get('initial') is not None:
+ defaults['initial'] = [i._get_pk_val() for i in defaults['initial']]
+ return super(ManyToManyField, self).formfield(**defaults)
class ManyToOneRel(object):
def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None,
Please sign in to comment.
Something went wrong with that request. Please try again.