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

Fix mapping for choice values #8968

Merged

Conversation

saadullahaleem
Copy link
Contributor

@saadullahaleem saadullahaleem commented May 1, 2023

Description

This pull request fixes the mapping between the human-labels and the database-values for the ChoiceField class. This PR has been mutated from 8955

Discussion 8965

@saadullahaleem saadullahaleem changed the title fix mapping for choice values Fix mapping for choice values May 1, 2023
@saadullahaleem saadullahaleem marked this pull request as ready for review May 1, 2023 18:07
@@ -1398,6 +1399,9 @@ def to_internal_value(self, data):
if data == '' and self.allow_blank:
return ''

if isinstance(data, IntegerChoices) and str(data) != str(data.value):
Copy link
Member

Choose a reason for hiding this comment

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

i'm happy with the handling of IntegerChoices but I would also love to see TextChoices and other buiilt in django enum choices compats as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@auvipy I've added TextChoices here too but should we have Enum too?

@auvipy auvipy added this to the 3.15 milestone May 2, 2023
Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

It is also possible to make use of the Enum Functional API with the caveat that labels are automatically generated -- its is from the django docs so we can consider that. may be on the original PR?

@@ -1824,6 +1826,31 @@ def test_edit_choices(self):
field.run_validation(2)
assert exc_info.value.detail == ['"2" is not a valid choice.']

def test_enum_choices(self):
class ChoiceCase(IntegerChoices):
Copy link
Member

Choose a reason for hiding this comment

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

we should add test for TextChoices too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@auvipy added

@@ -1406,6 +1411,11 @@ def to_internal_value(self, data):
def to_representation(self, value):
if value in ('', None):
return value

if isinstance(value, (IntegerChoices, TextChoices)) and str(value) != \
Copy link
Member

Choose a reason for hiding this comment

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

I have one concerns/question. should we consider both Int and text value as string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've typecasted both the __str__ representation of value and value.value for the check here.

@auvipy auvipy merged commit e08e606 into encode:master May 3, 2023
7 checks passed
@auvipy
Copy link
Member

auvipy commented May 3, 2023

congrats for you first contribution and welcome aboard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants