mirror of https://github.com/django/django.git
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:
parent
af377b13e1
commit
2e042f33e3
|
@ -334,10 +334,11 @@ class Field(object):
|
|||
return self._choices
|
||||
choices = property(_get_choices)
|
||||
|
||||
def formfield(self, initial=None):
|
||||
def formfield(self, **kwargs):
|
||||
"Returns a django.newforms.Field instance for this database Field."
|
||||
# TODO: This is just a temporary default during development.
|
||||
return forms.CharField(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.CharField(**defaults)
|
||||
|
||||
def value_from_object(self, obj):
|
||||
"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)
|
||||
cls._meta.has_auto_field = True
|
||||
|
||||
def formfield(self, initial=None):
|
||||
def formfield(self, **kwargs):
|
||||
return None
|
||||
|
||||
class BooleanField(Field):
|
||||
|
@ -396,8 +397,10 @@ class BooleanField(Field):
|
|||
def get_manipulator_field_objs(self):
|
||||
return [oldforms.CheckboxField]
|
||||
|
||||
def formfield(self, initial=None):
|
||||
return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
|
||||
defaults.update(kwargs)
|
||||
return forms.BooleanField(**defaults)
|
||||
|
||||
class CharField(Field):
|
||||
def get_manipulator_field_objs(self):
|
||||
|
@ -413,8 +416,10 @@ class CharField(Field):
|
|||
raise validators.ValidationError, gettext_lazy("This field cannot be null.")
|
||||
return str(value)
|
||||
|
||||
def formfield(self, initial=None):
|
||||
return forms.CharField(max_length=self.maxlength, required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
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.
|
||||
class CommaSeparatedIntegerField(CharField):
|
||||
|
@ -487,8 +492,10 @@ 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, initial=None):
|
||||
return forms.DateField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
|
||||
defaults.update(kwargs)
|
||||
return forms.DateField(**defaults)
|
||||
|
||||
class DateTimeField(DateField):
|
||||
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 ''),
|
||||
time_field: (val is not None and val.strftime("%H:%M:%S") or '')}
|
||||
|
||||
def formfield(self, initial=None):
|
||||
return forms.DateTimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
|
||||
defaults.update(kwargs)
|
||||
return forms.DateTimeField(**defaults)
|
||||
|
||||
class EmailField(CharField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
@ -565,8 +574,10 @@ class EmailField(CharField):
|
|||
def validate(self, field_data, all_data):
|
||||
validators.isValidEmail(field_data, all_data)
|
||||
|
||||
def formfield(self, initial=None):
|
||||
return forms.EmailField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
|
||||
defaults.update(kwargs)
|
||||
return forms.EmailField(**defaults)
|
||||
|
||||
class FileField(Field):
|
||||
def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
|
||||
|
@ -700,8 +711,10 @@ class IntegerField(Field):
|
|||
def get_manipulator_field_objs(self):
|
||||
return [oldforms.IntegerField]
|
||||
|
||||
def formfield(self, initial=None):
|
||||
return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
|
||||
defaults.update(kwargs)
|
||||
return forms.IntegerField(**defaults)
|
||||
|
||||
class IPAddressField(Field):
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
@ -757,8 +770,10 @@ class TextField(Field):
|
|||
def get_manipulator_field_objs(self):
|
||||
return [oldforms.LargeTextField]
|
||||
|
||||
def formfield(self, initial=None):
|
||||
return forms.CharField(required=not self.blank, widget=forms.Textarea, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name)}
|
||||
defaults.update(kwargs)
|
||||
return forms.CharField(**defaults)
|
||||
|
||||
class TimeField(Field):
|
||||
empty_strings_allowed = False
|
||||
|
@ -800,8 +815,10 @@ 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, initial=None):
|
||||
return forms.TimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
|
||||
defaults.update(kwargs)
|
||||
return forms.TimeField(**defaults)
|
||||
|
||||
class URLField(CharField):
|
||||
def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
|
||||
|
@ -817,8 +834,10 @@ class URLField(CharField):
|
|||
def get_internal_type(self):
|
||||
return "CharField"
|
||||
|
||||
def formfield(self, initial=None):
|
||||
return forms.URLField(required=not self.blank, verify_exists=self.verify_exists, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
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):
|
||||
def get_manipulator_field_objs(self):
|
||||
|
|
|
@ -548,8 +548,10 @@ class ForeignKey(RelatedField, Field):
|
|||
def contribute_to_related_class(self, cls, related):
|
||||
setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
|
||||
|
||||
def formfield(self, initial=None):
|
||||
return forms.ChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
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):
|
||||
def __init__(self, to, to_field=None, **kwargs):
|
||||
|
@ -612,8 +614,10 @@ class OneToOneField(RelatedField, IntegerField):
|
|||
if not cls._meta.one_to_one_field:
|
||||
cls._meta.one_to_one_field = self
|
||||
|
||||
def formfield(self, initial=None):
|
||||
return forms.ChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
|
||||
def formfield(self, **kwargs):
|
||||
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):
|
||||
def __init__(self, to, **kwargs):
|
||||
|
@ -725,12 +729,14 @@ class ManyToManyField(RelatedField, Field):
|
|||
"Returns the value of this field in the given model instance."
|
||||
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
|
||||
# MultipleChoiceField takes a list of IDs.
|
||||
if initial is not None:
|
||||
initial = [i._get_pk_val() for i in initial]
|
||||
return forms.MultipleChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
|
||||
if kwargs.get('initial') is not None:
|
||||
kwargs['initial'] = [i._get_pk_val() for i in kwargs['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):
|
||||
def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None,
|
||||
|
|
Loading…
Reference in New Issue