Ticket #12901: 12901-against-12454-2.diff

File 12901-against-12454-2.diff, 3.8 KB (added by Honza Král, 15 years ago)
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 0039516..866cacd 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)
     285
    279286            # Exclude fields that failed form validation. There's no need for
    280287            # the model fields to validate them as well.
    281288            elif field in self._errors.keys():
    282289                exclude.append(f.name)
     290
    283291            # Exclude empty fields that are not required by the form. The
    284292            # underlying model field may be required, so this keeps the model
    285293            # field from raising that error.
    class BaseInlineFormSet(BaseModelFormSet):  
    719727    def add_fields(self, form, index):
    720728        super(BaseInlineFormSet, self).add_fields(form, index)
    721729        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}
    723732        else:
    724733            # The foreign key field might not be on the form, so we poke at the
    725734            # Model field to get the label, since we need that for error messages.
     735            name = self.fk.name
    726736            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))
    728738            }
    729739            if self.fk.rel.field_name != self.fk.rel.to._meta.pk.name:
    730740                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)
    732750
    733751    def get_unique_error_message(self, unique_check):
    734752        unique_check = [field for field in unique_check if field != self.fk.name]
  • 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