Skip to content

Fix mapping for choice values#8968

Merged
auvipy merged 7 commits intoencode:masterfrom
saadullahaleem:feature/asymmetric-related-field
May 3, 2023
Merged

Fix mapping for choice values#8968
auvipy merged 7 commits intoencode:masterfrom
saadullahaleem:feature/asymmetric-related-field

Conversation

@saadullahaleem
Copy link
Copy Markdown
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
Comment thread rest_framework/fields.py Outdated
if data == '' and self.allow_blank:
return ''

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

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
Copy Markdown
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
Copy Markdown
Collaborator

@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?

Comment thread tests/test_fields.py
assert exc_info.value.detail == ['"2" is not a valid choice.']

def test_enum_choices(self):
class ChoiceCase(IntegerChoices):
Copy link
Copy Markdown
Collaborator

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@auvipy added

Comment thread rest_framework/fields.py
if value in ('', None):
return value

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

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
Copy Markdown
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
@auvipy
Copy link
Copy Markdown
Collaborator

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.

2 participants