Skip to content

RelatedField(required=False) throws KeyError when the data is missing #3985

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
6 tasks done
nazarewk opened this issue Mar 11, 2016 · 9 comments
Closed
6 tasks done

Comments

@nazarewk
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.)

Actually there is a duplicate but without a valid use case.

User story

I want to fill in the non-nullable relational field on the Model (eg. in save()), i would make it required=False in the DRF to allow it.

Steps to reproduce

from django.db import models
from rest_framework import serializers

class Target(models.Model):
    pass

class Source(models.Model):
    target = models.ForeignKey(Target)

class ExampleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Source
        fields = ('id', 'target')
        extra_kwargs = {'target': dict(required=False)}

serializer = ExampleSerializer(data={})
serializer.is_valid(raise_exception=True)

Expected behavior

assert 'target' not in serializer.data

Actual behavior

try:
    serializer.data
except KeyError:
    print('Nuff said')
@nazarewk nazarewk changed the title RelatedField(required=False) throws KeyError RelatedField(required=False) throws KeyError when the data is missing Mar 11, 2016
@carltongibson
Copy link
Collaborator

Can you try this with allow_null please? allow_empty is for lists

@xordoquy
Copy link
Collaborator

I'm a bit mixed on that one but would tend to consider that valid.
required is a field core argument so it should be properly handled. However, this rings a bell about doing something wrong here though I can't name it yet.

@nazarewk
Copy link
Author

@carltongibson actually looks like neither allow_empty nor allow_null make any difference.

@xordoquy: probably this #3976

@xordoquy
Copy link
Collaborator

@nazarewk not really it's a different thing.

@nazarewk
Copy link
Author

Well it feels weird for me to set the fields in either Model.clean_fields(), Model.save() or the likes, but sometimes it is extremely hard to avoid.

@carltongibson
Copy link
Collaborator

OK. This does seems a problem:

ModelSerializer(data={u'name': u'test'}):
    id = IntegerField(label='ID', read_only=True)
    name = CharField(max_length=100)
    target = PrimaryKeyRelatedField(help_text='Target', queryset=[...], required=False)

We get the KeyError for target here, as per the report. #3984 looks neat enough.

@sdlm
Copy link

sdlm commented May 26, 2016

I have this problem too.

behavior:
https://gist.github.com/sdlm/7890303821aa8cd5c91ea712f8f51be4

#3984 looks good !

@tomchristie
Copy link
Member

Okay, thanks for the data point - we'll try to get this reviewed soon.

@tomchristie
Copy link
Member

Closing with same resolution as #3984. Happy to reconsider if anyone believes that resolution isn't correct/sufficient.

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

5 participants