mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #24295 -- Allowed ModelForm meta to specify form field classes.
Thanks Carl Meyer and Markus Holtermann for the reviews.
This commit is contained in:
		| @@ -11,7 +11,7 @@ from django.core.exceptions import ( | ||||
| ) | ||||
| from django.core.files.uploadedfile import SimpleUploadedFile | ||||
| from django.core.validators import ValidationError | ||||
| from django.db import connection | ||||
| from django.db import connection, models | ||||
| from django.db.models.query import EmptyQuerySet | ||||
| from django.forms.models import ( | ||||
|     ModelFormMetaclass, construct_instance, fields_for_model, model_to_dict, | ||||
| @@ -545,6 +545,9 @@ class FieldOverridesByFormMetaForm(forms.ModelForm): | ||||
|                 ) | ||||
|             } | ||||
|         } | ||||
|         field_classes = { | ||||
|             'url': forms.URLField, | ||||
|         } | ||||
|  | ||||
|  | ||||
| class TestFieldOverridesByFormMeta(TestCase): | ||||
| @@ -588,7 +591,7 @@ class TestFieldOverridesByFormMeta(TestCase): | ||||
|     def test_error_messages_overrides(self): | ||||
|         form = FieldOverridesByFormMetaForm(data={ | ||||
|             'name': 'Category', | ||||
|             'url': '/category/', | ||||
|             'url': 'http://www.example.com/category/', | ||||
|             'slug': '!%#*@', | ||||
|         }) | ||||
|         form.full_clean() | ||||
| @@ -599,6 +602,11 @@ class TestFieldOverridesByFormMeta(TestCase): | ||||
|         ] | ||||
|         self.assertEqual(form.errors, {'slug': error}) | ||||
|  | ||||
|     def test_field_type_overrides(self): | ||||
|         form = FieldOverridesByFormMetaForm() | ||||
|         self.assertIs(Category._meta.get_field('url').__class__, models.CharField) | ||||
|         self.assertIsInstance(form.fields['url'], forms.URLField) | ||||
|  | ||||
|  | ||||
| class IncompleteCategoryFormWithFields(forms.ModelForm): | ||||
|     """ | ||||
|   | ||||
| @@ -1431,3 +1431,21 @@ class TestModelFormsetOverridesTroughFormMeta(TestCase): | ||||
|         form = BookFormSet.form(data={'title': 'Foo ' * 30, 'author': author.id}) | ||||
|         form.full_clean() | ||||
|         self.assertEqual(form.errors, {'title': ['Title too long!!']}) | ||||
|  | ||||
|     def test_modelformset_factory_field_class_overrides(self): | ||||
|         author = Author.objects.create(pk=1, name='Charles Baudelaire') | ||||
|         BookFormSet = modelformset_factory(Book, fields="__all__", field_classes={ | ||||
|             'title': forms.SlugField, | ||||
|         }) | ||||
|         form = BookFormSet.form(data={'title': 'Foo ' * 30, 'author': author.id}) | ||||
|         self.assertIs(Book._meta.get_field('title').__class__, models.CharField) | ||||
|         self.assertIsInstance(form.fields['title'], forms.SlugField) | ||||
|  | ||||
|     def test_inlineformset_factory_field_class_overrides(self): | ||||
|         author = Author.objects.create(pk=1, name='Charles Baudelaire') | ||||
|         BookFormSet = inlineformset_factory(Author, Book, fields="__all__", field_classes={ | ||||
|             'title': forms.SlugField, | ||||
|         }) | ||||
|         form = BookFormSet.form(data={'title': 'Foo ' * 30, 'author': author.id}) | ||||
|         self.assertIs(Book._meta.get_field('title').__class__, models.CharField) | ||||
|         self.assertIsInstance(form.fields['title'], forms.SlugField) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user