Ticket #8209: 8209_modelform_unique_validation.1.diff

File 8209_modelform_unique_validation.1.diff, 11.5 KB (added by brosner, 6 years ago)

added a patch for review

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 4563ace..77f4a36 100644
    a b Helper functions for creating Form classes from Django models 
    33and database field objects.
    44"""
    55
    6 from django.utils.translation import ugettext_lazy as _
    76from django.utils.encoding import smart_unicode
    87from django.utils.datastructures import SortedDict
     8from django.utils.text import get_text_list
     9from django.utils.translation import ugettext_lazy as _
    910
    1011from util import ValidationError, ErrorList
    1112from forms import BaseForm, get_declared_fields
    __all__ = ( 
    2021    'ModelMultipleChoiceField',
    2122)
    2223
     24try:
     25    any
     26except NameError:
     27    from django.utils.itercompat import any # pre-2.5
     28
    2329def save_instance(form, instance, fields=None, fail_message='saved',
    2430                  commit=True, exclude=None):
    2531    """
    class BaseModelForm(BaseForm): 
    202208            object_data.update(initial)
    203209        super(BaseModelForm, self).__init__(data, files, auto_id, prefix, object_data,
    204210                                            error_class, label_suffix, empty_permitted)
     211    def clean(self):
     212        self.validate_unique()
     213        return self.cleaned_data
     214   
     215    def validate_unique(self):
     216        from django.db.models.fields import FieldDoesNotExist
     217        unique_checks = list(self.instance._meta.unique_together[:])
     218        form_errors = []
     219        for name, field in self.fields.items():
     220            try:
     221                f = self.instance._meta.get_field_by_name(name)[0]
     222            except FieldDoesNotExist:
     223                # This is an extra field that's not on the model, ignore it
     224                continue
     225            if name in self.cleaned_data and f.unique:
     226                unique_checks.append((name,))
     227        for unique_check in [check for check in unique_checks if not any([x in self._errors for x in check])]:
     228            kwargs = dict([(field_name, self.cleaned_data[field_name]) for field_name in unique_check])
     229            qs = self.instance.__class__._default_manager.filter(**kwargs)
     230            if self.instance.pk is not None:
     231                qs = qs.exclude(pk=self.instance.pk)
     232            if qs.extra(select={'a': 1}).values('a').order_by():
     233                model_name = self.instance._meta.verbose_name.title()
     234                if len(unique_check) == 1:
     235                    field_name = unique_check[0]
     236                    field_label = self.fields[field_name].label
     237                    self._errors[field_name] = ErrorList([
     238                        _("%(model_name)s with this %(field_label)s already exists.") % \
     239                        {'model_name': model_name, 'field_label': field_label}
     240                    ])
     241                else:
     242                    field_labels = [self.fields[field_name].label for field_name in unique_check]
     243                    field_labels = get_text_list(field_labels, _('and'))
     244                    form_errors.append(
     245                        _("%(model_name)s with this %(field_label)s already exists.") % \
     246                        {'model_name': model_name, 'field_label': field_labels}
     247                    )
     248                for field_name in unique_check:
     249                    del self.cleaned_data[field_name]
     250        if form_errors:
     251            raise ValidationError(form_errors)
    205252
    206253    def save(self, commit=True):
    207254        """
    class BaseModelFormSet(BaseFormSet): 
    246293                 queryset=None, **kwargs):
    247294        self.queryset = queryset
    248295        defaults = {'data': data, 'files': files, 'auto_id': auto_id, 'prefix': prefix}
    249         if self.max_num > 0:
    250             qs = self.get_queryset()[:self.max_num]
    251         else:
    252             qs = self.get_queryset()
    253         defaults['initial'] = [model_to_dict(obj) for obj in qs]
     296        defaults['initial'] = [model_to_dict(obj) for obj in self.get_queryset()]
    254297        defaults.update(kwargs)
    255298        super(BaseModelFormSet, self).__init__(**defaults)
    256 
     299   
     300    def _construct_form(self, i, **kwargs):
     301        if i < self._initial_form_count:
     302            kwargs['instance'] = self.get_queryset()[i]
     303        return super(BaseModelFormSet, self)._construct_form(i, **kwargs)
     304       
    257305    def get_queryset(self):
    258         if self.queryset is not None:
    259             return self.queryset
    260         return self.model._default_manager.get_query_set()
    261 
     306        if not hasattr(self, '_queryset'):
     307            if self.queryset is not None:
     308                qs = self.queryset
     309            else:
     310                qs = self.model._default_manager.get_query_set()
     311            if self.max_num > 0:
     312                self._queryset = qs[:self.max_num]
     313            else:
     314                self._queryset = qs
     315        return self._queryset
     316   
    262317    def save_new(self, form, commit=True):
    263318        """Saves and returns a new model instance for the given form."""
    264319        return save_instance(form, self.model(), exclude=[self._pk_field.name], commit=commit)
    class BaseInlineFormSet(BaseModelFormSet): 
    358413            self._total_form_count = self._initial_form_count
    359414            self._initial_form_count = 0
    360415        super(BaseInlineFormSet, self)._construct_forms()
     416   
     417    def _construct_form(self, i, **kwargs):
     418        form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
     419        if self.save_as_new:
     420            # kill the pk from form data so that it doesn't cause a unique
     421            # validation error when trying to re-save the forms.
     422            form.data[form.add_prefix(self._pk_field.name)] = None
     423        return form
    361424
    362425    def get_queryset(self):
    363426        """
  • django/utils/itercompat.py

    diff --git a/django/utils/itercompat.py b/django/utils/itercompat.py
    index c166da3..1b24cc9 100644
    a b def sorted(in_value): 
    7272    out_value = in_value[:]
    7373    out_value.sort()
    7474    return out_value
     75
     76def any(seq):
     77    """any implementation."""
     78    for x in seq:
     79        if seq:
     80            return True
     81    return False
  • docs/topics/forms/modelforms.txt

    diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt
    index d161b3f..163c428 100644
    a b parameter when declaring the form field:: 
    338338   ...     class Meta:
    339339   ...         model = Article
    340340
     341Overriding the clean() method
     342-----------------------------
     343
     344You can overide the ``clean()`` method on a model form to provide additional
     345validation in the same way you can on a normal form.  However, by default the
     346``clean()`` method validates the uniqueness of fields that are marked as unique
     347on the model, and those marked as unque_together, if you would like to overide
     348the ``clean()`` method and maintain the default validation you must call the
     349parent class's ``clean()`` method.
     350
    341351Form inheritance
    342352----------------
    343353
    books of a specific author. Here is how you could accomplish this:: 
    500510    >>> from django.forms.models import inlineformset_factory
    501511    >>> BookFormSet = inlineformset_factory(Author, Book)
    502512    >>> author = Author.objects.get(name=u'Orson Scott Card')
    503     >>> formset = BookFormSet(instance=author)
    504  No newline at end of file
     513    >>> formset = BookFormSet(instance=author)
  • tests/modeltests/model_forms/models.py

    diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
    index 5f714fb..9a306aa 100644
    a b class CommaSeparatedInteger(models.Model): 
    117117    def __unicode__(self):
    118118        return self.field
    119119
     120class Product(models.Model):
     121    slug = models.SlugField(unique=True)
     122   
     123    def __unicode__(self):
     124        return self.slug
     125
     126class Price(models.Model):
     127    price = models.DecimalField(max_digits=10, decimal_places=2)
     128    quantity = models.PositiveIntegerField()
     129   
     130    def __unicode__(self):
     131        return u"%s for %s" % (self.quantity, self.price)
     132   
     133    class Meta:
     134        unique_together = (('price', 'quantity'),)
     135
    120136class ArticleStatus(models.Model):
    121137    status = models.CharField(max_length=2, choices=ARTICLE_STATUS_CHAR, blank=True, null=True)
    122138
     139
    123140__test__ = {'API_TESTS': """
    124141>>> from django import forms
    125142>>> from django.forms.models import ModelForm, model_to_dict
    u'1,,2' 
    11321149>>> f.clean('1')
    11331150u'1'
    11341151
    1135 # Choices on CharField and IntegerField
     1152# unique/unique_together validation
    11361153
     1154>>> class ProductForm(ModelForm):
     1155...     class Meta:
     1156...         model = Product
     1157>>> form = ProductForm({'slug': 'teddy-bear-blue'})
     1158>>> form.is_valid()
     1159True
     1160>>> obj = form.save()
     1161>>> obj
     1162<Product: teddy-bear-blue>
     1163>>> form = ProductForm({'slug': 'teddy-bear-blue'})
     1164>>> form.is_valid()
     1165False
     1166>>> form._errors
     1167{'slug': [u'Product with this Slug already exists.']}
     1168>>> form = ProductForm({'slug': 'teddy-bear-blue'}, instance=obj)
     1169>>> form.is_valid()
     1170True
     1171
     1172# ModelForm test of unique_together constraint
     1173>>> class PriceForm(ModelForm):
     1174...     class Meta:
     1175...         model = Price
     1176>>> form = PriceForm({'price': '6.00', 'quantity': '1'})
     1177>>> form.is_valid()
     1178True
     1179>>> form.save()
     1180<Price: 1 for 6.00>
     1181>>> form = PriceForm({'price': '6.00', 'quantity': '1'})
     1182>>> form.is_valid()
     1183False
     1184>>> form._errors
     1185{'__all__': [u'Price with this Price and Quantity already exists.']}
     1186
     1187# Choices on CharField and IntegerField
    11371188>>> class ArticleForm(ModelForm):
    11381189...     class Meta:
    11391190...         model = Article
  • tests/modeltests/model_formsets/models.py

    diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py
    index 332c5a7..1e25baa 100644
    a b class Restaurant(Place): 
    7373    def __unicode__(self):
    7474        return self.name
    7575
     76class Product(models.Model):
     77    slug = models.SlugField(unique=True)
     78
     79    def __unicode__(self):
     80        return self.slug
     81
     82class Price(models.Model):
     83    price = models.DecimalField(max_digits=10, decimal_places=2)
     84    quantity = models.PositiveIntegerField()
     85
     86    def __unicode__(self):
     87        return u"%s for %s" % (self.quantity, self.price)
     88
     89    class Meta:
     90        unique_together = (('price', 'quantity'),)
     91
    7692class MexicanRestaurant(Restaurant):
    7793    serves_tacos = models.BooleanField()
    7894
    True 
    553569>>> type(_get_foreign_key(MexicanRestaurant, Owner))
    554570<class 'django.db.models.fields.related.ForeignKey'>
    555571
     572# unique/unique_together validation ###########################################
     573
     574>>> FormSet = modelformset_factory(Product, extra=1)
     575>>> data = {
     576...     'form-TOTAL_FORMS': '1',
     577...     'form-INITIAL_FORMS': '0',
     578...     'form-0-slug': 'car-red',
     579... }
     580>>> formset = FormSet(data)
     581>>> formset.is_valid()
     582True
     583>>> formset.save()
     584[<Product: car-red>]
     585
     586>>> data = {
     587...     'form-TOTAL_FORMS': '1',
     588...     'form-INITIAL_FORMS': '0',
     589...     'form-0-slug': 'car-red',
     590... }
     591>>> formset = FormSet(data)
     592>>> formset.is_valid()
     593False
     594>>> formset.errors
     595[{'slug': [u'Product with this Slug already exists.']}]
     596
     597# unique_together
     598
     599>>> FormSet = modelformset_factory(Price, extra=1)
     600>>> data = {
     601...     'form-TOTAL_FORMS': '1',
     602...     'form-INITIAL_FORMS': '0',
     603...     'form-0-price': u'12.00',
     604...     'form-0-quantity': '1',
     605... }
     606>>> formset = FormSet(data)
     607>>> formset.is_valid()
     608True
     609>>> formset.save()
     610[<Price: 1 for 12.00>]
     611
     612>>> data = {
     613...     'form-TOTAL_FORMS': '1',
     614...     'form-INITIAL_FORMS': '0',
     615...     'form-0-price': u'12.00',
     616...     'form-0-quantity': '1',
     617... }
     618>>> formset = FormSet(data)
     619>>> formset.is_valid()
     620False
     621>>> formset.errors
     622[{'__all__': [u'Price with this Price and Quantity already exists.']}]
     623
    556624"""}
Back to Top