Updated NestedBoundField to also handle empty string when rendering its form by Ernest0x · Pull Request #3677 · encode/django-rest-framework (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation10 Commits2 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

Ernest0x

If a NestedBoundField field has a value of None and is inside another NestedBoundField field, it will have its value converted to an empty string while the form of its enclosing field is being rendered. So, NestedBoundField fields with an empty string value must be handled the same way as NestedBoundField fields with a None value.

@Ernest0x

…ts form

If a NestedBoundField field has a value of None and is inside another NestedBoundField field, it will have its value converted to an empty string while the form of its enclosing field is being rendered. So, NestedBoundField fields with an empty string value must be handled the same way as NestedBoundField fields with a None value.

@tomchristie

Could you give a simple example, demonstrating exactly what gets rendered incorrectly, and how this fix affects it?

@Ernest0x

Could you give a simple example, demonstrating exactly what gets rendered incorrectly, and how this fix affects it?

Of course. I have made the following #3464 (comment)

@tomchristie

Okay. That's something, tho a minimal code example would still be helpful.

@andrewdodd

I agree, I'd love to see a basic example of how this occurs.

@Ernest0x

Ok, here is code that reproduces exactly the error that this pull request fixes:

from rest_framework import serializers from rest_framework.renderers import HTMLFormRenderer

class Level2NestedSerializer(serializers.Serializer): some_field = serializers.CharField()

class Level1NestedSerializer(serializers.Serializer): nested2_field = Level2NestedSerializer(allow_null=True) another_field = serializers.CharField()

class Level0NestedSerializer(serializers.Serializer): nested1_field = Level1NestedSerializer()

serializer = Level0NestedSerializer(data={ 'nested1_field': { 'nested2_field': None, 'another_field': 'test' } })

serializer.is_valid()

renderer = HTMLFormRenderer() for field in serializer: rendered = renderer.render_field(field, {})

@tomchristie

Okay, and how does the HTML form render before/after the fix. (Include screenshots if you're feeling generous! 😎 )

@Ernest0x

Okay, and how does the HTML form render before/after the fix. (Include screenshots if you're feeling generous! )

Before the fix, it does not render at all. It crashes with a traceback.
After the fix, it renders ok. For the nested field with None value, an empty form is rendered.

@tomchristie

Would you consider pulling together a test case? Failing that we might consider merging in any case and adding that as an outstanding issue.

@Ernest0x

@Ernest0x

Would you consider pulling together a test case?

Sure. Added in this pull request.

tomchristie added a commit that referenced this pull request

Nov 27, 2015

@tomchristie

Updated NestedBoundField to also handle empty string when rendering its form

@tomchristie

This was referenced

Mar 9, 2017