Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 2e042f33e319bb5c4ea48b24f2520df29d30a886 1 parent af377b1
Adrian Holovaty authored
63  django/db/models/fields/__init__.py
@@ -334,10 +334,11 @@ def _get_choices(self):
334 334
             return self._choices
335 335
     choices = property(_get_choices)
336 336
 
337  
-    def formfield(self, initial=None):
  337
+    def formfield(self, **kwargs):
338 338
         "Returns a django.newforms.Field instance for this database Field."
339  
-        # TODO: This is just a temporary default during development.
340  
-        return forms.CharField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
  339
+        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
  340
+        defaults.update(kwargs)
  341
+        return forms.CharField(**defaults)
341 342
 
342 343
     def value_from_object(self, obj):
343 344
         "Returns the value of this field in the given model instance."
@@ -379,7 +380,7 @@ def contribute_to_class(self, cls, name):
379 380
         super(AutoField, self).contribute_to_class(cls, name)
380 381
         cls._meta.has_auto_field = True
381 382
 
382  
-    def formfield(self, initial=None):
  383
+    def formfield(self, **kwargs):
383 384
         return None
384 385
 
385 386
 class BooleanField(Field):
@@ -396,8 +397,10 @@ def to_python(self, value):
396 397
     def get_manipulator_field_objs(self):
397 398
         return [oldforms.CheckboxField]
398 399
 
399  
-    def formfield(self, initial=None):
400  
-        return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
  400
+    def formfield(self, **kwargs):
  401
+        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
  402
+        defaults.update(kwargs)
  403
+        return forms.BooleanField(**defaults)
401 404
 
402 405
 class CharField(Field):
403 406
     def get_manipulator_field_objs(self):
@@ -413,8 +416,10 @@ def to_python(self, value):
413 416
                 raise validators.ValidationError, gettext_lazy("This field cannot be null.")
414 417
         return str(value)
415 418
 
416  
-    def formfield(self, initial=None):
417  
-        return forms.CharField(max_length=self.maxlength, required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
  419
+    def formfield(self, **kwargs):
  420
+        defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name)}
  421
+        defaults.update(kwargs)
  422
+        return forms.CharField(**defaults)
418 423
 
419 424
 # TODO: Maybe move this into contrib, because it's specialized.
420 425
 class CommaSeparatedIntegerField(CharField):
@@ -487,8 +492,10 @@ def flatten_data(self, follow, obj=None):
487 492
         val = self._get_val_from_obj(obj)
488 493
         return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')}
489 494
 
490  
-    def formfield(self, initial=None):
491  
-        return forms.DateField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
  495
+    def formfield(self, **kwargs):
  496
+        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
  497
+        defaults.update(kwargs)
  498
+        return forms.DateField(**defaults)
492 499
 
493 500
 class DateTimeField(DateField):
494 501
     def to_python(self, value):
@@ -548,8 +555,10 @@ def flatten_data(self,follow, obj = None):
548 555
         return {date_field: (val is not None and val.strftime("%Y-%m-%d") or ''),
549 556
                 time_field: (val is not None and val.strftime("%H:%M:%S") or '')}
550 557
 
551  
-    def formfield(self, initial=None):
552  
-        return forms.DateTimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
  558
+    def formfield(self, **kwargs):
  559
+        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
  560
+        defaults.update(kwargs)
  561
+        return forms.DateTimeField(**defaults)
553 562
 
554 563
 class EmailField(CharField):
555 564
     def __init__(self, *args, **kwargs):
@@ -565,8 +574,10 @@ def get_manipulator_field_objs(self):
565 574
     def validate(self, field_data, all_data):
566 575
         validators.isValidEmail(field_data, all_data)
567 576
 
568  
-    def formfield(self, initial=None):
569  
-        return forms.EmailField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
  577
+    def formfield(self, **kwargs):
  578
+        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
  579
+        defaults.update(kwargs)
  580
+        return forms.EmailField(**defaults)
570 581
 
571 582
 class FileField(Field):
572 583
     def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
@@ -700,8 +711,10 @@ class IntegerField(Field):
700 711
     def get_manipulator_field_objs(self):
701 712
         return [oldforms.IntegerField]
702 713
 
703  
-    def formfield(self, initial=None):
704  
-        return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
  714
+    def formfield(self, **kwargs):
  715
+        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
  716
+        defaults.update(kwargs)
  717
+        return forms.IntegerField(**defaults)
705 718
 
706 719
 class IPAddressField(Field):
707 720
     def __init__(self, *args, **kwargs):
@@ -757,8 +770,10 @@ class TextField(Field):
757 770
     def get_manipulator_field_objs(self):
758 771
         return [oldforms.LargeTextField]
759 772
 
760  
-    def formfield(self, initial=None):
761  
-        return forms.CharField(required=not self.blank, widget=forms.Textarea, label=capfirst(self.verbose_name), initial=initial)
  773
+    def formfield(self, **kwargs):
  774
+        defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name)}
  775
+        defaults.update(kwargs)
  776
+        return forms.CharField(**defaults)
762 777
 
763 778
 class TimeField(Field):
764 779
     empty_strings_allowed = False
@@ -800,8 +815,10 @@ def flatten_data(self,follow, obj = None):
800 815
         val = self._get_val_from_obj(obj)
801 816
         return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')}
802 817
 
803  
-    def formfield(self, initial=None):
804  
-        return forms.TimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
  818
+    def formfield(self, **kwargs):
  819
+        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)}
  820
+        defaults.update(kwargs)
  821
+        return forms.TimeField(**defaults)
805 822
 
806 823
 class URLField(CharField):
807 824
     def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
@@ -817,8 +834,10 @@ def get_manipulator_field_objs(self):
817 834
     def get_internal_type(self):
818 835
         return "CharField"
819 836
 
820  
-    def formfield(self, initial=None):
821  
-        return forms.URLField(required=not self.blank, verify_exists=self.verify_exists, label=capfirst(self.verbose_name), initial=initial)
  837
+    def formfield(self, **kwargs):
  838
+        defaults = {'required': not self.blank, 'verify_exists': self.verify_exists, 'label': capfirst(self.verbose_name)}
  839
+        defaults.update(kwargs)
  840
+        return forms.URLField(**defaults)
822 841
 
823 842
 class USStateField(Field):
824 843
     def get_manipulator_field_objs(self):
22  django/db/models/fields/related.py
@@ -548,8 +548,10 @@ def contribute_to_class(self, cls, name):
@@ -612,8 +614,10 @@ def contribute_to_related_class(self, cls, related):
@@ -725,12 +729,14 @@ def value_from_object(self, obj):

0 notes on commit 2e042f3

Please sign in to comment.
Something went wrong with that request. Please try again.