Ticket #12901: 12901-against-12454.diff

File 12901-against-12454.diff, 2.0 KB (added by Honza_Kral, 5 years ago)

test case and fix

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 0039516..d914f89 100644
    a b class BaseModelForm(BaseForm): 
    276276            # adding these values to the model after form validation.
    277277            if field not in self.fields:
    278278                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)
    279285            # Exclude fields that failed form validation. There's no need for
    280286            # the model fields to validate them as well.
    281287            elif field in self._errors.keys():
  • tests/modeltests/model_forms/models.py

    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. 
    99
    1010import os
    1111import tempfile
     12from unittest import TestCase
    1213
    1314from django.db import models
    1415from django.core.files.storage import FileSystemStorage
     16from django import forms
     17from django.forms.models import ModelForm
    1518
    1619temp_storage_dir = tempfile.mkdtemp()
    1720temp_storage = FileSystemStorage(temp_storage_dir)
    class BigInt(models.Model): 
    205208    def __unicode__(self):
    206209        return unicode(self.biggie)
    207210
     211class IncompleteCategoryForm(ModelForm):
     212    url = forms.CharField(required=False)
     213
     214    class Meta:
     215        fields = ('name', 'slug')
     216        model = Category
     217
     218class 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
    208223__test__ = {'API_TESTS': """
    209224>>> from django import forms
    210225>>> from django.forms.models import ModelForm, model_to_dict
Back to Top