Skip to content

Fixed RelatedField(required=False) bug #3984

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

Closed
wants to merge 3 commits into from
Closed

Fixed RelatedField(required=False) bug #3984

wants to merge 3 commits into from

Conversation

nazarewk
Copy link

Description

I encountered the issue in totally unrelated (and invalid according to docs) case, but also found valid use case where it fails in exactly the same way.

When we want to fill-in the field specifically on Model level instead of ModelSerializer we would make it required=False, allow_empty=True and not pass the field at all in data.

Before this fix resolution of Serializer.data would yield KeyError.

@tomchristie
Copy link
Member

When we want to fill-in the field specifically on Model level instead of ModelSerializer

For this case you should make the field read_only=True on the serializer.

@tomchristie tomchristie closed this Jun 6, 2016
@cgthayer
Copy link

@tomchristie if you do read_only=True then you can't pass it in if you wish to, right? For "required=False" I'd expect I could omit the key and value from data, or include it.

@mazharrazmian
Copy link

I still get the error. I have an AssetSerializer and a PriaryKeyRelatedField with required=false in it, named 'asset_devices'. When i don't provide devices data, it gives me key error.

If this has been fixed, and I am using the latest version of DRF, why am I still getting this error?

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

Successfully merging this pull request may close these issues.

4 participants