Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Permit mixed casing of string values for BooleanField validation#8970

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

Already on GitHub?Sign in to your account

Conversation

@neckenth
Copy link
Contributor

Description

In this PR, all values of typestr being serialized tobool for fields typedBooleanField are first cast to lowercase andthen validated against the lists ofTRUE_VALUES,FALSE_VALUES, andNULL_VALUES. Discovered this restriction when a user tried to send "Yes" as a valid value for a Boolean field to my team's Public-facing API.

@auvipyauvipy self-requested a reviewMay 4, 2023 04:01
Copy link
Collaborator

@auvipyauvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I need more detail here. how django handle boolean values?

@encodeencode deleted a comment fromphichaweeMay 4, 2023
@saadullahaleem
Copy link
Contributor

This is a good fix.

I initially thought that Django itself simply works withbool to cast astr but that's not the case. Here's Django'sBooleanField.

classBooleanField(Field):widget=CheckboxInputdefto_python(self,value):"""Return a Python boolean object."""# Explicitly check for the string 'False', which is what a hidden field# will submit for False. Also check for '0', since this is what# RadioSelect will provide. Because bool("True") == bool('1') == True,# we don't need to handle that explicitly.ifisinstance(value,str)andvalue.lower()in ("false","0"):value=Falseelse:value=bool(value)returnsuper().to_python(value)defvalidate(self,value):ifnotvalueandself.required:raiseValidationError(self.error_messages["required"],code="required")defhas_changed(self,initial,data):ifself.disabled:returnFalse# Sometimes data or initial may be a string equivalent of a boolean# so we should run it through to_python first to get a boolean valuereturnself.to_python(initial)!=self.to_python(data)

Django performs explicit checks too. The comments suggest that they're there to handle output of the form fields.

@auvipyauvipy requested a review fromkevin-brownMay 7, 2023 13:11
@auvipyauvipy removed the request for review fromkevin-brownJune 12, 2023 15:19
@auvipyauvipy added this to the3.15 milestoneJun 12, 2023
@auvipyauvipy merged commita180bde intoencode:masterJun 12, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@auvipyauvipyauvipy approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

3.15

Development

Successfully merging this pull request may close these issues.

3 participants

@neckenth@saadullahaleem@auvipy

[8]ページ先頭

©2009-2025 Movatter.jp