From b09bdd2ae8177325f414cf4ec116e8c8e17cd4c0 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 28 Apr 2007 13:55:24 +0000 Subject: [PATCH] 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 --- django/db/models/fields/__init__.py | 44 ++++++++++++++--------------- django/db/models/fields/related.py | 18 ++++++------ 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 91629cd679..a074e2bdab 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -341,11 +341,11 @@ class Field(object): 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 @@ class BooleanField(Field): 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 @@ class CharField(Field): 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 @@ class DateField(Field): 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 @@ class DateTimeField(DateField): 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 @@ class EmailField(CharField): 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 @@ class IntegerField(Field): 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 @@ class PhoneNumberField(IntegerField): 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 @@ class TextField(Field): 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 @@ class TimeField(Field): 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 @@ class URLField(CharField): 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): diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index e8152f32e7..0739d0461a 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -553,9 +553,9 @@ class ForeignKey(RelatedField, Field): 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 @@ class OneToOneField(RelatedField, IntegerField): 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 @@ class ManyToManyField(RelatedField, Field): 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,