The Wayback Machine - https://web.archive.org/web/20200604133133/https://github.com/encode/django-rest-framework/issues/7246
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

Improve ChoiceField.choices documentation #7246

Open
chgad opened this issue Mar 26, 2020 · 2 comments
Open

Improve ChoiceField.choices documentation #7246

chgad opened this issue Mar 26, 2020 · 2 comments

Comments

@chgad
Copy link

@chgad chgad commented Mar 26, 2020

Hi,
i just now stumbled upon that the ChoiceField accepts the choices as tuples (value, display_name) like documented here. As in this issue #4082, i was very surprised that the representation is the value not the display_nameprovided in the choices.
So:

  1. It would be nice to document this explicitly.
  2. Why is the behavior defined this way?

I'd expect the display_name as the representation for a given value why else should i define the display_name then? The issue mentioned above was briefly answered with a quote form the django docs which, in my eyes, would point towards my expected behavior.
Especially if your API serves a detached frontend app. This app would need the choices-dict too for the correct display of the possible abstract values. Thus leading to code duplication.

@denolehov
Copy link

@denolehov denolehov commented Mar 28, 2020

You can find choice values and display values in the response from OPTIONS (metadata) request.

I would say that serializing choice field values to values instead of display_names makes more sense in the context of APIs, since they're intended to be used programmatically.

If your particular use-case requires display_names, you can inherit choice field and override its .to_representation() method.

@rpkilby rpkilby changed the title Document ChoiceField does not show `display_name` Improve ChoiceField.choices documentation Apr 1, 2020
@rpkilby
Copy link
Member

@rpkilby rpkilby commented Apr 10, 2020

@denolehov is correct. The value is intended to be used programmatically, while display_name is intended for display purposes. Display values aren't suitable for API use, as they may be translated based on the user's locale.

I've gone into more detail in #6854 (comment), but in short, it would be nice if there was some construct that differentiated DB values, API representation, and display values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.