Allows field's choices to be a callable

Starting in Django 5 field's choices can also be a callable
This commit is contained in:
Alisson Patricio 2024-02-06 23:52:44 -03:00 committed by GitHub
parent 96c09ac439
commit a48abd1d33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,6 @@
import inspect import inspect
from collections import OrderedDict from collections import OrderedDict
from collections.abc import Callable
from functools import partial, singledispatch, wraps from functools import partial, singledispatch, wraps
from django.db import models from django.db import models
@ -72,6 +73,8 @@ def convert_choice_name(name):
def get_choices(choices): def get_choices(choices):
converted_names = [] converted_names = []
if isinstance(choices, Callable):
choices = choices()
if isinstance(choices, OrderedDict): if isinstance(choices, OrderedDict):
choices = choices.items() choices = choices.items()
for value, help_text in choices: for value, help_text in choices: