#36930: Using a custom ModelChoices (not IntegerChoices or TextChoices) member
as
the default value of a model field doesn't select the default value in
form's select
-------------------------------------+-------------------------------------
Reporter: Antoine Humbert | Owner: Antoine
| Humbert
Type: Bug | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Antoine Humbert:
Old description:
> This was triggered in the following forum post:
> https://forum.djangoproject.com/t/default-for-choices-field-isnt-default-
> in-admin-form/44189
>
> As per the documentation and the **date** / **DateField** example (see
> https://docs.djangoproject.com/en/6.0/ref/models/fields/#enumeration-
> types), defining choices for a data type other than **int** or **str**
> (e.g. float) should be :
>
> {{{
> class FactorChoices(float, models.Choices):
> HALF = 0.5
> SINGLE = 1
> DOUBLE = 2
> }}}
>
> then, setting a default value for a field should use one of the
> enumeration's member (as per the example from documentation:
> `default=YearInSchool.FRESHMAN`). E.g.:
>
> {{{
> class SomeModel(models.Model):
> factor = models.FloatField(choices=FactorChoices,
> default=FactorChoices.SINGLE)
> }}}
>
> When using such definitions, the default value is not selected by default
> in forms created for such model. This is bescause the `str()` of the
> default value returns the string representation provided by `enum.Enum`
> instead of the string representation from `float`.
>
> I believe this is a regression in
> https://github.com/antoinehumbert/django/commit/fe19b33e2f76045617067dd5123041ae4d3a91ee
> where the `django.db.models.Choices.__str__` (which specifically called
> `str(self.value)`) method was removed to the profit of making
> `IntegerChoices` and `TextChoices` inherit from `enum.ReprEnum`).
> Unfortunaltly, this dropped the initial `Choices.__str__` behaviour for
> all other subclasses of `Choices`.
>
> I think the fix is as simple as making `Choices` inherit from
> `enum.ReprEnum` instead of `enum.Enum`.
>
> I'm working on a PR.
New description:
This was triggered in the following forum post:
https://forum.djangoproject.com/t/default-for-choices-field-isnt-default-
in-admin-form/44189
As per the documentation and the **date** / **DateField** example (see
https://docs.djangoproject.com/en/6.0/ref/models/fields/#enumeration-
types), defining choices for a data type other than **int** or **str**
(e.g. float) should be :
{{{
class FactorChoices(float, models.Choices):
HALF = 0.5
SINGLE = 1
DOUBLE = 2
}}}
then, setting a default value for a field should use one of the
enumeration's member (as per the example from documentation:
`default=YearInSchool.FRESHMAN`). E.g.:
{{{
class SomeModel(models.Model):
factor = models.FloatField(choices=FactorChoices,
default=FactorChoices.SINGLE)
}}}
When using such definitions, the default value is not selected by default
in forms created for such model. This is bescause the `str()` of the
default value returns the string representation provided by `enum.Enum`
instead of the string representation from `float`.
I believe this is a regression in
https://github.com/django/django/commit/fe19b33e2f76045617067dd5123041ae4d3a91ee
where the `django.db.models.Choices.__str__` (which specifically called
`str(self.value)`) method was removed to the profit of making
`IntegerChoices` and `TextChoices` inherit from `enum.ReprEnum`).
Unfortunaltly, this dropped the initial `Choices.__str__` behaviour for
all other subclasses of `Choices`.
I think the fix is as simple as making `Choices` inherit from
`enum.ReprEnum` instead of `enum.Enum`.
I'm working on a PR.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/36930#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/0107019c611fb301-fb2779c3-2eb8-4b35-9f3b-5617eef22d0e-000000%40eu-central-1.amazonses.com.