jazzband / django-model-utils

Django model mixins and utilities.
https://django-model-utils.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.67k stars 366 forks source link

Django 5.0: Choices not working #616

Open MiltosD opened 7 months ago

MiltosD commented 7 months ago

Problem

In Django 5.0 field choices seem to be always lists, even when defined as Choices. In Django 4.3.9 it seems to be working as expected.

Environment

Code examples

from model_utils import Choices

class MyClass
    my_field = models.CharField(
           choices=Choices(("M", "Male", "MALE"), ("F", "Female", "FEMALE"))
    )

Django 4.2.11

from models import MyModel
type(MyModel._meta.get_field("my_field").choices)
<class 'model_utils.choices.Choices'>
MyModel._meta.get_field("my_field").choices
Choices(("M", "Male", "MALE"), ("F", "Female", "FEMALE"))

Django 5.0

from models import MyModel
type(MyModel._meta.get_field("my_field").choices)
<class 'list'>
MyModel._meta.get_field("my_field").choices
[("M", "MALE"), ("F", "FEMALE")]

This raises an error

TypeError: list indices must be integers or slices, not str

when trying to

MyModel._meta.get_field("my_field").choices["M"]
foarsitter commented 5 months ago

Can you provide a PR with a failing testcase?