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

Nested path for PATCH operations ? #69

Open
askz opened this issue Sep 20, 2017 · 2 comments
Open

Nested path for PATCH operations ? #69

askz opened this issue Sep 20, 2017 · 2 comments

Comments

@askz
Copy link
Contributor

askz commented Sep 20, 2017

imagine I have a profile linked to my user and I want to expose only one route (/users/) to patch the twos, is it actually possible ?
Seems like the actual implementation isn't compatible ? is it ?

I created two tuples and merged them with :
PATH_CHOICES = USER_CHOICES (as /%s) + PROFILE_CHOICES (as /profile/%s)

But I get this error :

marshmallow.exceptions.ValidationError: Field 'parts_number' does not exist, so it cannot be patched
@askz
Copy link
Contributor Author

askz commented Sep 22, 2017

So, I came up with :

    @classmethod
    def replace(cls, obj, field, value, state):
        if 'profile/' in field:
            sub_obj, sub_field = field.split('/')
            sub_obj = getattr(obj, sub_obj)
            if hasattr(sub_obj, sub_field):
                obj = sub_obj
                field = sub_field
        if not hasattr(obj, field):
            raise ValidationError("Field '%s' does not exist, so it cannot be patched" % field)
        setattr(obj, field, value)
        return True

This is obviously temporary and hard-coded for my specific use case.
But in the future I'll probably need it for more modules, and will make a PR if I have the time !

I'm open to suggestions though.

@frol
Copy link
Owner

frol commented Sep 24, 2017

@askz I am sorry for the late response! I think, your case is quite legal, though I had never had such a requirement yet. I will be happy to accept a PR. Your ad-hoc implementation is quite reasonable to me.

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

No branches or pull requests

2 participants