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

ChoiceField with required=False not optional in HTML view #2184

Closed
brianmay opened this issue Dec 3, 2014 · 5 comments · Fixed by #2239
Closed

ChoiceField with required=False not optional in HTML view #2184

brianmay opened this issue Dec 3, 2014 · 5 comments · Fixed by #2239
Labels
Milestone

Comments

@brianmay
Copy link

brianmay commented Dec 3, 2014

Hello,

I have a db model containing:

action = models.CharField(max_length=4, null=True, blank=True, choices=PHOTO_ACTION, db_index=True)

ModelSerializer I believe turns this into a ChoiceField.

Unfortunately the choice list doesn't list an appropriate value to correspond with None.

I have tried setting required=False, but it has no affect.

Thanks.

@tomchristie
Copy link
Member

tomchristie commented Dec 3, 2014

Looks like we may currently support this in allow_null, but not allow_blank. We should probably support both options.

https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/templates/rest_framework/horizontal/select.html#L7

Still surprising tho as you do also have null=True on that model field.

It'd be helpful to see the serializer field it generates - could you do print(repr(MySerializer)) in the Django shell and include the output for the action field on this ticket.

@brianmay
Copy link
Author

brianmay commented Dec 3, 2014

Unfortunately, that is not very helpful:

In [5]: print(repr(spud.serializers.PhotoSerializer))
<class 'spud.serializers.PhotoSerializer'> 

Ok, from within the program, did a print(repr(serializer.get_fields())) instead.

That gives me:

('action', ChoiceField(choices=[(u'D', u'delete'), (u'S', u'regenerate size'), (u'R', u'regenerate thumbnails'), (u'V', u'regenerate video'), (u'M', u'move photo'), (u'auto', u'rotate automatic'), (u'90', u'rotate 90 degrees clockwise'), (u'180', u'rotate 180 degrees clockwise'), (u'270', u'rotate 270 degrees clockwise')], required=False)), 

Which lists all the choices except for the None choice.

Is this what you where after?

@tomchristie
Copy link
Member

tomchristie commented Dec 5, 2014

print(repr(spud.serializers.PhotoSerializer()))

You were missing the () to instantiate the serializer. Above would have worked.

Is this what you where after?

Yes, thank you.

@tomchristie
Copy link
Member

tomchristie commented Dec 5, 2014

To do: Add allow_blank for ChoiceField, and ensure it gets mapped to from model field -> serializer field.

@jpadilla
Copy link
Member

jpadilla commented Dec 9, 2014

Working on this, but having trouble figuring out what tests should be added.

tomchristie added a commit that referenced this issue Dec 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants