Ticket #9245: issue9245.diff

File issue9245.diff, 6.5 KB (added by ambv, 3 years ago)

Patch for 1.4RC

  • django/trunk/tests/regressiontests/model_fields/tests.py

    diff --git a/django/trunk/tests/regressiontests/model_fields/tests.py b/django/trunk/tests/regressiontests/model_fields/tests.py
    a b  
    1111from django.utils import unittest
    1212
    1313from .models import (Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post,
    14     NullBooleanModel, BooleanModel, Document, RenamedField)
     14    NullBooleanModel, BooleanModel, Document, RenamedField,
     15    CustomIntegerFormField)
    1516
    1617# If PIL available, do these tests.
    1718if Image:
     
    223224        self.assertEqual(Whiz(c=None).get_c_display(), None)    # Blank value
    224225        self.assertEqual(Whiz(c='').get_c_display(), '')        # Empty value
    225226
     227    def test_choices_formfield_overrides(self):
     228        whiz_meta = Whiz(c=1)._meta
     229        field = whiz_meta.get_field_by_name('c')[0]
     230        self.assertIs(type(field.formfield()),
     231                forms.TypedChoiceField)
     232        self.assertIs(type(whiz_meta.get_field_by_name('d')[0].formfield()),
     233                CustomIntegerFormField)
     234        self.assertIs(type(whiz_meta.get_field_by_name('ts')[0].formfield()),
     235                forms.DateTimeField)
     236
    226237class SlugFieldTests(test.TestCase):
    227238    def test_slugfield_max_length(self):
    228239        """
  • django/trunk/tests/regressiontests/model_fields/models.py

    diff --git a/django/trunk/tests/regressiontests/model_fields/models.py b/django/trunk/tests/regressiontests/model_fields/models.py
    a b  
    1313    except ImportError:
    1414        Image = None
    1515
     16from django import forms
    1617from django.core.files.storage import FileSystemStorage
    1718from django.db import models
    1819from django.db.models.fields.files import ImageFieldFile, ImageField
     
    2930    b = models.CharField(max_length=10)
    3031    a = models.ForeignKey(Foo, default=get_foo)
    3132
     33class CustomIntegerFormField(forms.IntegerField):
     34    override_for_choices = False
     35
     36    def __init__(self, *args, **kwargs):
     37        if 'choices' in kwargs:
     38            del kwargs['choices']
     39        super(CustomIntegerFormField, self).__init__(*args, **kwargs)
     40
     41class CustomIntegerField(models.IntegerField):
     42    def formfield(self, **kwargs):
     43        if 'form_class' not in kwargs:
     44            kwargs['form_class'] = CustomIntegerFormField
     45        return super(CustomIntegerField, self).formfield(**kwargs)
     46
    3247class Whiz(models.Model):
    3348    CHOICES = (
    3449        ('Group 1', (
     
    4459        (0,'Other'),
    4560    )
    4661    c = models.IntegerField(choices=CHOICES, null=True)
     62    d = CustomIntegerField(choices=CHOICES, default=4)
     63    ts = models.DateTimeField(auto_now_add=True)
    4764
    4865class BigD(models.Model):
    4966    d = models.DecimalField(max_digits=38, decimal_places=30)
  • django/trunk/django/db/models/fields/__init__.py

    diff --git a/django/trunk/django/db/models/fields/__init__.py b/django/trunk/django/db/models/fields/__init__.py
    a b  
    471471            include_blank = (self.blank or
    472472                             not (self.has_default() or 'initial' in kwargs))
    473473            defaults['choices'] = self.get_choices(include_blank=include_blank)
    474             defaults['coerce'] = self.to_python
    475             if self.null:
    476                 defaults['empty_value'] = None
    477             form_class = forms.TypedChoiceField
    478             # Many of the subclass-specific formfield arguments (min_value,
    479             # max_value) don't apply for choice fields, so be sure to only pass
    480             # the values that TypedChoiceField will understand.
    481             for k in kwargs.keys():
    482                 if k not in ('coerce', 'empty_value', 'choices', 'required',
    483                              'widget', 'label', 'initial', 'help_text',
    484                              'error_messages', 'show_hidden_initial'):
    485                     del kwargs[k]
     474            if form_class.override_for_choices:
     475                defaults['coerce'] = self.to_python
     476                if self.null:
     477                    defaults['empty_value'] = None
     478                form_class = forms.TypedChoiceField
     479                # Many of the subclass-specific formfield arguments (min_value,
     480                # max_value) don't apply for choice fields, so be sure to only
     481                # pass the values that TypedChoiceField will understand.
     482                for k in kwargs.keys():
     483                    if k not in ('coerce', 'empty_value', 'choices', 'required',
     484                                'widget', 'label', 'initial', 'help_text',
     485                                'error_messages', 'show_hidden_initial'):
     486                        del kwargs[k]
    486487        defaults.update(kwargs)
    487488        return form_class(**defaults)
    488489
  • django/trunk/django/forms/fields.py

    diff --git a/django/trunk/django/forms/fields.py b/django/trunk/django/forms/fields.py
    a b  
    5050        'required': _(u'This field is required.'),
    5151        'invalid': _(u'Enter a valid value.'),
    5252    }
     53    override_for_choices = False
    5354
    5455    # Tracks each time a Field instance is created. Used to retain order.
    5556    creation_counter = 0
     
    182183        return result
    183184
    184185class CharField(Field):
     186    override_for_choices = True
     187
    185188    def __init__(self, max_length=None, min_length=None, *args, **kwargs):
    186189        self.max_length, self.min_length = max_length, min_length
    187190        super(CharField, self).__init__(*args, **kwargs)
     
    209212        'max_value': _(u'Ensure this value is less than or equal to %(limit_value)s.'),
    210213        'min_value': _(u'Ensure this value is greater than or equal to %(limit_value)s.'),
    211214    }
     215    override_for_choices = True
    212216
    213217    def __init__(self, max_value=None, min_value=None, *args, **kwargs):
    214218        self.max_value, self.min_value = max_value, min_value
     
    665669    cleaned to None.
    666670    """
    667671    widget = NullBooleanSelect
     672    override_for_choices = True
    668673
    669674    def to_python(self, value):
    670675        """
Back to Top