﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34388	Added support for direct usage of Choices classes on model fields	T. Franzel	T. Franzel	"Hi,

I would like to propose a feature addition on how `Choices` are handled when used on model fields. Currently, `Field.choices` only accepts iterables. This has 2 shortcommings imho:

1. - Rejecting a `Choices` class as argument to `Field(choices=...)` seems counter-intuitive. Providing the class directly currently results in a `fields.E005` error. 
    - To make this more pythonic, the field should also accept the Choice class directly and deal with the variation internally. 
    - I really can't come up with a scenario where a user would want a different behavior or rather that a user would be surprised by the implicit resolution.

~~2. By forcing the user to expand the `Choices` class manually, essentially all meta information is lost. Downstream packages may benefit from this lost information.~~
   - ~~Specifically, as maintainer of [https://github.com/tfranzel/drf-spectacular drf-spectcular] (OpenAPI generator for DRF), I am interested in the name of the choice set (e.g. Suit, Vehicle, Gender) and potentially also the docstring. This would greatly improve OpenAPI generation of choice sets and take out the unnecessary guesswork to find a proper name. (And if anyone wonders, the model field name is not a good candidate for a choice set name.)~~
 
This PR allows to use `Choices` classes directly as argument, while being transparent. No behavioral changes otherwise.

I marked this as `dev`, but it would be awesome if it could still slip into `4.2`. Not sure if the feature window is still open, but this is more or less a trivial and backwards-compatible change with little risk. PR is still missing some docs, which I will write if this is considered. 


{{{ #!python
class Suit(models.IntegerChoices):
    """""" All possible card categories in a deck """"""
    DIAMOND = 1, _(""Diamond"")
    SPADE = 2, _(""Spade"")
    HEART = 3, _(""Heart"")
    CLUB = 4, _(""Club"")
    
class Choiceful(models.Model):
    # CURRENTLY:
    from_enum_old = models.IntegerField(choices=Suit.choices)
    
    # NEW: raised an fields.E005 prior to proposed PR. Now, retains reference 
    # to class and transparently resolves via implicit `.choices` call
    from_new = models.IntegerField(choices=Suit)
}}}"	New feature	closed	Database layer (models, ORM)	dev	Normal	fixed		David Wobrock Adam Johnson	Ready for checkin	1	0	0	0	0	0
