Skip to content

Fix unique_for_<range> validation on partial serializer updates (refs #6341) - #10040

Open
VimalN2005 wants to merge 2 commits into
encode:mainfrom
VimalN2005:fix-unique-for-date-partial-update
Open

Fix unique_for_<range> validation on partial serializer updates (refs #6341)#10040
VimalN2005 wants to merge 2 commits into
encode:mainfrom
VimalN2005:fix-unique-for-date-partial-update

Conversation

@VimalN2005

Copy link
Copy Markdown

Note: Before submitting a code change, please review our contributing guidelines.

Description

Fixes #6341.

BaseUniqueForValidator (and subclasses UniqueForDateValidator, UniqueForMonthValidator, and UniqueForYearValidator) previously raised a required field ValidationError on partial updates (partial=True) whenever field or date_field was omitted from the request payload.

Furthermore, if only one of the fields was provided in the partial update, the validator failed because the missing field was not resolved from serializer.instance.

Changes

  • Updated BaseUniqueForValidator.enforce_required_fields to accept serializer=None and bypass requirement checks when updating an existing instance, matching the design of UniqueTogetherValidator.
  • Updated BaseUniqueForValidator.__call__ to:
    • Skip validation on partial updates when neither unique field is provided in attrs.
    • Resolve the missing field from serializer.instance when only one field is provided.
    • Safely normalize string dates to date objects if the instance attribute holds a raw date string.
    • Skip query validation if both field values are unchanged on the instance.
  • Added comprehensive regression tests in tests/test_validators.py covering:
    • Partial updates with omitted fields.
    • Partial updates modifying only the unique field (both unique and collision cases).
    • Partial updates modifying only the date field.
    • Partial updates resubmitting unchanged values.
    • UniqueForMonthValidator and UniqueForYearValidator parity.
    • Models utilizing HiddenField with date constraints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Model unique_for_month changes ability to partially update ModelSerializer

1 participant