Ticket #9245: 9245.diff
File 9245.diff, 6.3 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/__init__.py
313 313 include_blank = self.blank or not (self.has_default() or 'initial' in kwargs) 314 314 defaults['choices'] = self.get_choices(include_blank=include_blank) 315 315 defaults['coerce'] = self.to_python 316 if self.null: 317 defaults['empty_value'] = None 318 form_class = forms.TypedChoiceField 316 if form_class in (forms.CharField, forms.BooleanField,forms.IntegerField, forms.RegexField, 317 forms.TypedChoiceField, forms.NullBooleanField): 318 form_class = forms.TypedChoiceField 319 if self.null: 320 defaults['empty_value'] = None 321 #else: 319 322 # Many of the subclass-specific formfield arguments (min_value, 320 323 # max_value) don't apply for choice fields, so be sure to only pass 321 324 # the values that TypedChoiceField will understand. -
tests/regressiontests/forms/tests.py
30 30 from widgets import tests as widgets_tests 31 31 from formsets import tests as formset_tests 32 32 from media import media_tests 33 from forms import custom_form_field_tests 33 34 34 35 __test__ = { 35 36 'extra_tests': extra_tests, … … 63 64 'media_tests': media_tests, 64 65 'util_tests': util_tests, 65 66 'widgets_tests': widgets_tests, 67 'custom_form_field_tests': custom_form_field_tests, 66 68 } 67 69 68 70 if __name__ == "__main__": -
tests/regressiontests/forms/models.py
5 5 # Can't import as "forms" due to implementation details in the test suite (the 6 6 # current file is called "forms" and is already imported). 7 7 from django import forms as django_forms 8 from django.utils.encoding import force_unicode 8 9 9 10 class BoundaryModel(models.Model): 10 11 positive_integer = models.PositiveIntegerField(null=True, blank=True) … … 24 25 class FileForm(django_forms.Form): 25 26 file1 = django_forms.FileField() 26 27 28 # models for custom form fields, regress for #9245 29 from django.forms import fields 30 class CustomFormField(fields.TypedChoiceField): 31 def __init__(self, *args, **kwargs): 32 super(CustomFormField, self).__init__(*args, **kwargs) 33 34 def clean(self, value): 35 return super(CustomFormField, self).clean(value) 36 37 # a trivial object to be stored in db 38 class Small(object): 39 def __init__(self, first, second): 40 self.first, self.second = first, second 41 42 def __unicode__(self): 43 return u'%s%s' % (force_unicode(self.first), force_unicode(self.second)) 44 45 def __str__(self): 46 return unicode(self).encode('utf-8') 47 48 # create a custom field upon 'Small' and associate with a custom form field 49 class SmallField(models.Field): 50 __metaclass__ = models.SubfieldBase 51 52 def __init__(self, *args, **kwargs): 53 kwargs['max_length'] = 2 54 super(SmallField, self).__init__(*args, **kwargs) 55 56 def get_internal_type(self): 57 return 'CharField' 58 59 def to_python(self, value): 60 if isinstance(value, Small): 61 return value 62 return Small(value[0], value[1]) 63 64 def get_db_prep_save(self, value): 65 return unicode(value) 66 67 def get_db_prep_lookup(self, lookup_type, value): 68 if lookup_type == 'exact': 69 return force_unicode(value) 70 if lookup_type == 'in': 71 return [force_unicode(v) for v in value] 72 if lookup_type == 'isnull': 73 return [] 74 raise FieldError('Invalid lookup type: %r' % lookup_type) 75 76 def formfield(self, **kwargs): 77 return super(SmallField, self).formfield(form_class = CustomFormField, **kwargs) 78 return super(SmallField, self).formfield(**kwargs) 79 80 # use SmallField in a model 81 class MyModel(models.Model): 82 name = models.CharField(max_length=10) 83 MY_CHOICES = ( 84 ('AB', 'ab'), 85 ('CD', 'cd'), 86 ('EF', 'ef'), 87 ) 88 data = SmallField('small field',choices = MY_CHOICES) 89 90 def __unicode__(self): 91 return force_unicode(self.name) 92 27 93 __test__ = {'API_TESTS': """ 28 94 >>> from django.forms.models import ModelForm 29 95 >>> from django.core.files.uploadedfile import SimpleUploadedFile -
tests/regressiontests/forms/forms.py
1750 1750 True 1751 1751 1752 1752 """ 1753 # -*- coding: utf-8 -*- 1754 custom_form_field_tests = r""" 1755 >>> from regressiontests.forms.models import MyModel 1756 >>> from regressiontests.forms.models import Small 1757 >>> s = Small('A', 'B') 1758 >>> MyModel.objects.create(pk=1, name='abc', data = s) 1759 <MyModel: abc> 1760 >>> from django.forms import ModelForm 1761 >>> class MyModelForm(ModelForm): 1762 ... class Meta: 1763 ... model = MyModel 1764 ... 1765 >>> mymodel_form = MyModelForm(instance = MyModel.objects.get(id=1)) 1766 >>> print mymodel_form['data'] 1767 <select name="data" id="id_data"> 1768 <option value="">---------</option> 1769 <option value="AB" selected="selected">ab</option> 1770 <option value="CD">cd</option> 1771 <option value="EF">ef</option> 1772 </select> 1773 >>> from regressiontests.forms.models import SmallField 1774 >>> small_field = SmallField('a simple small field', choices = MyModel.MY_CHOICES) 1775 >>> small_field.formfield().__class__ 1776 <class 'regressiontests.forms.models.CustomFormField'> 1777 >>> small_field = SmallField('small field without choices') 1778 >>> small_field.formfield().__class__ 1779 <class 'regressiontests.forms.models.CustomFormField'> 1780 >>> from django.db import models 1781 >>> regular_field = models.CharField(max_length=2, choices = MyModel.MY_CHOICES) 1782 >>> regular_field.formfield().__class__ 1783 <class 'django.forms.fields.TypedChoiceField'> 1784 >>> regular_field = models.CharField(max_length=10) 1785 >>> regular_field.formfield().__class__ 1786 <class 'django.forms.fields.CharField'> 1787 """