Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Getting KeyError on partial update (PATCH) when 'tags' not included #7

Closed
joeweiss opened this issue May 16, 2015 · 3 comments
Closed

Comments

@joeweiss
Copy link
Contributor

Partials update (via PATCH request) is throwing a KeyError in _pop_tags when the 'tags' field's data is not included in the patch request.

In our situation, we patch a lot of records, and we only patch the fields that change.

Current implementation

    def _pop_tags(self, validated_data):
        to_be_tagged = {}

        for key in self.fields.keys():
            field = self.fields[key]
            if isinstance(field, TagListSerializerField):
                to_be_tagged[key] = validated_data.pop(key)

        return (to_be_tagged, validated_data)

We need the check the existence of the key before pop'ing it, correct?

    def _pop_tags(self, validated_data):
        to_be_tagged = {}

        for key in self.fields.keys():
            field = self.fields[key]
            if isinstance(field, TagListSerializerField):
                if key in validated_data:
                    to_be_tagged[key] = validated_data.pop(key)

        return (to_be_tagged, validated_data)

I'm happy to open a PR, but wanted to make sure I wasn't missing something simple or overthinking it.

@glemmaPaul
Copy link
Owner

Yeah you're absolutely right! It's strange I've never added partial=True. I'll add tests for that too. You can open a PR if you would like, thank you for bringing it up 👍

@joeweiss
Copy link
Contributor Author

Great. I opened a PR. It needs tests though. Wasn't sure how you wanted those handled.

@joeweiss
Copy link
Contributor Author

Hey folks. This issue is occurring again.

It looks like the fix for this issue was overwritten after the PR was accepted.

Here's where it happened:
2909a7f#diff-332a5a3794e86d0e4a5433137ec60215L48

I'm opening another PR now. Thanks!

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

No branches or pull requests

2 participants