Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix read_only + default unique_together validation.#5922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@carltongibson
Copy link
Collaborator

In order to validate aunique_together relation on aread_only field, you need to specify adefault on the field to be used for validation purposes.

e.g.:

    owner = serializers.PrimaryKeyRelatedField(        read_only=True,        default=serializers.CreateOnlyDefault(serializers.CurrentUserDefault()),    )

Whereowner is part of aunique_together relation on the model.

#5886 unwittingly broke this behaviour:

  • TheUniqueTogetherValidator was not added to the serialiser.
  • Nor was thedefault value used in validation.

This PR:

  • Adds a test case covering the expected behaviour. (That passes on 3.7.7 but fails on 3.8.0)
  • Ensures theUniqueTogetherValidator is correctly generated
  • Uses the default value for read_only fields (where specified)only for validation, in line with the changes forAlter read_only+default behaviour #5886.

@carltongibson
Copy link
CollaboratorAuthor

@akx Any chance you could install this branch and see if your problem is resolved? Thanks.

@xordoquy@rpkilby: If you have capacity your eye over this would be helpful. Thanks.

@carltongibsoncarltongibson mentioned this pull requestApr 6, 2018
2 tasks
@akx
Copy link
Contributor

akx commentedApr 6, 2018

@carltongibson Yup, seems to work. 🏆

carltongibson reacted with hooray emoji

Copy link
Contributor

@lovelydinosaurlovelydinosaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yup, looks pretty reasonable to me.

@carltongibsoncarltongibson merged commit42eb5a4 intoencode:masterApr 6, 2018
@carltongibsoncarltongibson deleted the 382/read_only-default-validation branchApril 6, 2018 13:20
@carltongibson
Copy link
CollaboratorAuthor

v3.8.2 is now available on PyPI. Thanks all.

default=field.get_default()
exceptSkipField:
continue
defaults[field.field_name]=default

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

if we are using a field with field_name different from the field in model that is used as source then this is failing and it should be

defaults[field.source] = default

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I somehow missed your comment on this, but you're correct. The code above specifically checks for a compatible field source, but uses the serializerfield_name instead of thesource model field.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

i had reported this and tried to work on it, but couldnt continue
and along with this issue other issue is there defined in the Issue below
#7005
#7003

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Hi@anveshagarwal. I'm currently following up on that PR. That's what prompted my comment. 😄

anveshagarwal reacted with laugh emoji
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

For posterity's sake, my reasoning above was slightly wrong. Thesource checks are an indicator that we should be operating onfield.source instead offield.field_name, however the way to determine this is by looking atrun_validation. Before it callsrun_validators (then_read_only_defaults), it callsto_internal_value. At this point, the data has been converted to its source structure. So, it makes sense that the read-only defaults would also map to its source structure, instead of the raw field names.

pchiquet pushed a commit to pchiquet/django-rest-framework that referenced this pull requestNov 17, 2020
* Add test for read_only + default unique_together validation.* Fix read_only + default validation
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@lovelydinosaurlovelydinosaurlovelydinosaur approved these changes

+2 more reviewers

@rpkilbyrpkilbyrpkilby left review comments

@anveshagarwalanveshagarwalanveshagarwal left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

3.8.2 Release

Development

Successfully merging this pull request may close these issues.

5 participants

@carltongibson@akx@lovelydinosaur@rpkilby@anveshagarwal

[8]ページ先頭

©2009-2025 Movatter.jp