Opened 4 years ago

Closed 4 years ago

#32706 closed Cleanup/optimization (invalid)

Ambiguity around field choices in the Model Field section

Reported by: Logan Kilpatrick Owned by: nobody
Component: Documentation Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am reading here: https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.Field.choices about field choices. The docs remark that:

"Generally, it’s best to define choices inside a model class, and to define a suitably-named constant for each value:"

Why is this? Looking at the code, it is not abundantly clear to me as a developer why this is the case. I think folks would benefit from a sentence or two of commentary as to why this approach is generally better.

Change History (1)

comment:1 by Mariusz Felisiak, 4 years ago

Resolution: invalid
Status: newclosed

This is briefly explained in the same paragraph, just below the example:

Though you can define a choices list outside of a model class and then refer to it, defining the choices and names for each choice inside the model class keeps all of that information with the class that uses it, and helps reference the choices (e.g, Student.SOPHOMORE will work anywhere that the Student model has been imported).

I think it's sufficient.

Note: See TracTickets for help on using tickets.
Back to Top