Ticket #14531: validate_unique.patch

File validate_unique.patch, 1.8 KB (added by wogan, 4 years ago)

Outline of suggested approach

  • django/forms/models.py

     
    202202        self.fields = getattr(options, 'fields', None)
    203203        self.exclude = getattr(options, 'exclude', None)
    204204        self.widgets = getattr(options, 'widgets', None)
     205        self.validate_unique = getattr(options, 'validate_unique', None) # change to True when deprecation ends
    205206
    206207
    207208class ModelFormMetaclass(type):
     
    262263        # if initial was provided, it should override the values from instance
    263264        if initial is not None:
    264265            object_data.update(initial)
    265         # self._validate_unique will be set to True by BaseModelForm.clean().
    266         # It is False by default so overriding self.clean() and failing to call
    267         # super will stop validate_unique from being called.
    268         self._validate_unique = False
    269266        super(BaseModelForm, self).__init__(data, files, auto_id, prefix, object_data,
    270267                                            error_class, label_suffix, empty_permitted)
    271268
     
    323320        return exclude
    324321
    325322    def clean(self):
    326         self._validate_unique = True
     323        opts = self._meta
     324        if opts.validate_unique is None:
     325            opts.validate_unique = True
    327326        return self.cleaned_data
    328327
    329328    def _post_clean(self):
     
    357356            self._update_errors({NON_FIELD_ERRORS: e.messages})
    358357
    359358        # Validate uniqueness if needed.
    360         if self._validate_unique:
     359        if opts.validate_unique:
    361360            self.validate_unique()
     361        elif opts.validate_unique is None:
     362            raise PendingDeprecationWarning('?')
    362363
    363364    def validate_unique(self):
    364365        """
Back to Top