Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add changeable option for fields. #6897

Closed
5 of 6 tasks
calvin620707 opened this issue Aug 29, 2019 · 2 comments
Closed
5 of 6 tasks

Add changeable option for fields. #6897

calvin620707 opened this issue Aug 29, 2019 · 2 comments

Comments

@calvin620707
Copy link

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Purpose

This is a feature request to add a new option, changeable, for fields to decide whether the field is changeable during put/patch.

Proposal

import requests
from rest_framework import serializers

class FooSerializer(serializers.Serializer):
    unchangeable = serializers.CharField(changeable=False)

Expected behavior

resp = requests.post('/foo', json={'unchangeable': 'abc'})
assert resp.status_code == 200

foo_id = resp.json()['id']

resp = requests.patch(f"/foo/{foo_id}", json={'unchangeable': 'changed!'})
assert resp.status_code == 400
assert resp.json() == {'errors': {'unchangeable': 'This field is unchangeable'}}

resp = requests.patch(f'/foo/{foo_id}', json={'unchangeable': 'abc'})
assert resp.status_code == 200

# expected the same behavior for put.

Pseudo code

class Field(object):
    def __init__(self, ..., changeable=True):
        self.changeable = changeable
        # ...
    
    def validate_changeable(self, data):
        if self.changeable or self.parent is None or self.parent.instance is None:
            return
        
        original_data = getattr(self.parent.instance, self.field_name)
        if self.compare:
            is_changed = self.compare(original_data, data) != 0
        else:
            is_changed = original_data != data
        
        if is_changed:
            raise ValidationError("This field is unchangeable.")

    def run_valdations(self, data=empty):
        self.validate_changeable(data)
        ...

I can help to send the MR for this feature if the feature is acceptable.

@rpkilby
Copy link
Member

rpkilby commented Sep 3, 2019

Hi @calvin620707. I don't think this is something we'd want to merge into the core framework, as it seems like a fairly niche case with no single correct implementation. e.g., another user might have a different expectation in that an unchangeable field should effectively become read-only for updates, and that including the field in the request body should be invalid.

My recommendation would be to implement this as a validator class, or as a utility function that can be called from the validate_<field> hook. Note that the validator API will change with #6172.

@rpkilby rpkilby closed this as completed Sep 3, 2019
@calvin620707
Copy link
Author

validator class seems like a reusable way. Thanks for pointing that out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants