Skip to content

Uniqueness validators should not be run for excluded (read_only) fields #2848

Closed

Description

@ntucker

get_uniqueness_extra_kwargs and get_unique_together_validators both rely on Django's model unique_together specification. However, if read_only is set for both they should not be expected to be validated as normal. This reflects the behavior of django forms (exclude the field in form and it doesn't validated against this). This allows unique_together fields to be autogenerated in the save method of the model.

Activity

  1. lovelydinosaur commented on Apr 21, 2015

    @lovelydinosaur
    Member

    Presumably if either is read_only, then we shouldn't validate them? (Ie. same as excluding one?)

  2. ntucker commented on Apr 21, 2015

    @ntucker
    Author

    This is how django does it: https://github.com/django/django/blob/master/django/forms/models.py#L344 (build exclusion list)

    Then here: https://github.com/django/django/blob/a10b4c010ab2cdaa6ba8bfaec3e3540299ea77be/django/db/models/base.py#L926
    for name in check:
    # If this is an excluded field, don't add this check.
    if name in exclude:
    break

    So It looks like you are correct, a uniquetogether check is not done if ANY of the fields are missing

  3. ntucker commented on Apr 24, 2015

    @ntucker
    Author

    I can write a PR for this as long as the design is accepted. I just hate writing stuff that won't get approved.

  4. lovelydinosaur commented on Jun 25, 2015

    @lovelydinosaur
    Member

    I'm broadly in favor of this, but I don't think we can make any definite decisions without seeing an example fix and test case.

    Labeling this as a valid bug to address tho.

  5. modified the milestone: on Jul 8, 2015
  6. modified the milestones: , on Jul 16, 2015
  7. modified the milestones: , 3.2.1 Release on Jul 30, 2015
  8. 21 remaining items

  9. modified the milestones: 3.3.3 Release, , 3.4.0 Release on Feb 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions