diff --git a/django/forms/models.py b/django/forms/models.py
index 0039516..866cacd 100644
a
|
b
|
class BaseModelForm(BaseForm):
|
276 | 276 | # adding these values to the model after form validation. |
277 | 277 | if field not in self.fields: |
278 | 278 | exclude.append(f.name) |
| 279 | |
| 280 | # if some fields were excluded, don't run their validation |
| 281 | # this is needed on top of previous line to catch fields added onto |
| 282 | # the form with same name as some model field |
| 283 | elif self._meta.fields and field not in self._meta.fields: |
| 284 | exclude.append(f.name) |
| 285 | |
279 | 286 | # Exclude fields that failed form validation. There's no need for |
280 | 287 | # the model fields to validate them as well. |
281 | 288 | elif field in self._errors.keys(): |
282 | 289 | exclude.append(f.name) |
| 290 | |
283 | 291 | # Exclude empty fields that are not required by the form. The |
284 | 292 | # underlying model field may be required, so this keeps the model |
285 | 293 | # field from raising that error. |
… |
… |
class BaseInlineFormSet(BaseModelFormSet):
|
719 | 727 | def add_fields(self, form, index): |
720 | 728 | super(BaseInlineFormSet, self).add_fields(form, index) |
721 | 729 | if self._pk_field == self.fk: |
722 | | form.fields[self._pk_field.name] = InlineForeignKeyField(self.instance, pk_field=True) |
| 730 | name = self._pk_field.name |
| 731 | kwargs = {'pk_field': True} |
723 | 732 | else: |
724 | 733 | # The foreign key field might not be on the form, so we poke at the |
725 | 734 | # Model field to get the label, since we need that for error messages. |
| 735 | name = self.fk.name |
726 | 736 | kwargs = { |
727 | | 'label': getattr(form.fields.get(self.fk.name), 'label', capfirst(self.fk.verbose_name)) |
| 737 | 'label': getattr(form.fields.get(name), 'label', capfirst(self.fk.verbose_name)) |
728 | 738 | } |
729 | 739 | if self.fk.rel.field_name != self.fk.rel.to._meta.pk.name: |
730 | 740 | kwargs['to_field'] = self.fk.rel.field_name |
731 | | form.fields[self.fk.name] = InlineForeignKeyField(self.instance, **kwargs) |
| 741 | |
| 742 | form.fields[name] = InlineForeignKeyField(self.instance, **kwargs) |
| 743 | |
| 744 | # add the generated field to form._meta.fields if defined to make sure |
| 745 | # validation isn't skipped on that field |
| 746 | if form._meta.fields: |
| 747 | if isinstance(form._meta.fields, tuple): |
| 748 | form._meta.fields = list(form._meta.fields) |
| 749 | form._meta.fields.append(self.fk.name) |
732 | 750 | |
733 | 751 | def get_unique_error_message(self, unique_check): |
734 | 752 | unique_check = [field for field in unique_check if field != self.fk.name] |
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index b8b826a..12a39b5 100644
a
|
b
|
words, most of these tests should be rewritten.
|
9 | 9 | |
10 | 10 | import os |
11 | 11 | import tempfile |
| 12 | from unittest import TestCase |
12 | 13 | |
13 | 14 | from django.db import models |
14 | 15 | from django.core.files.storage import FileSystemStorage |
| 16 | from django import forms |
| 17 | from django.forms.models import ModelForm |
15 | 18 | |
16 | 19 | temp_storage_dir = tempfile.mkdtemp() |
17 | 20 | temp_storage = FileSystemStorage(temp_storage_dir) |
… |
… |
class BigInt(models.Model):
|
205 | 208 | def __unicode__(self): |
206 | 209 | return unicode(self.biggie) |
207 | 210 | |
| 211 | class IncompleteCategoryForm(ModelForm): |
| 212 | url = forms.CharField(required=False) |
| 213 | |
| 214 | class Meta: |
| 215 | fields = ('name', 'slug') |
| 216 | model = Category |
| 217 | |
| 218 | class TestCase(TestCase): |
| 219 | def test_category_doesnt_validate_url_when_not_on_modelform(self): |
| 220 | form = IncompleteCategoryForm(data={'name': 'some name', 'slug': 'some-slug'}) |
| 221 | assert form.is_valid() |
| 222 | |
208 | 223 | __test__ = {'API_TESTS': """ |
209 | 224 | >>> from django import forms |
210 | 225 | >>> from django.forms.models import ModelForm, model_to_dict |