mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-24 02:24:10 +03:00
Allows field's choices to be a callable (#1497)
* Allows field's choices to be a callable Starting in Django 5 field's choices can also be a callable * test if field with callable choices converts into enum --------- Co-authored-by: Kien Dang <mail@kien.ai>
This commit is contained in:
parent
ac09cd2967
commit
45c2aa09b5
|
@ -1,4 +1,5 @@
|
|||
import inspect
|
||||
from collections.abc import Callable
|
||||
from functools import partial, singledispatch, wraps
|
||||
|
||||
from django.db import models
|
||||
|
@ -71,6 +72,8 @@ def convert_choice_name(name):
|
|||
|
||||
def get_choices(choices):
|
||||
converted_names = []
|
||||
if isinstance(choices, Callable):
|
||||
choices = choices()
|
||||
|
||||
# In restframework==3.15.0, choices are not passed
|
||||
# as OrderedDict anymore, so it's safer to check
|
||||
|
|
|
@ -180,6 +180,26 @@ def test_field_with_choices_convert_enum():
|
|||
assert graphene_type._meta.enum.__members__["EN"].description == "English"
|
||||
|
||||
|
||||
def test_field_with_callable_choices_convert_enum():
|
||||
def get_choices():
|
||||
return ("es", "Spanish"), ("en", "English")
|
||||
|
||||
field = models.CharField(help_text="Language", choices=get_choices)
|
||||
|
||||
class TranslatedModel(models.Model):
|
||||
language = field
|
||||
|
||||
class Meta:
|
||||
app_label = "test"
|
||||
|
||||
graphene_type = convert_django_field_with_choices(field).type.of_type
|
||||
assert graphene_type._meta.name == "TestTranslatedModelLanguageChoices"
|
||||
assert graphene_type._meta.enum.__members__["ES"].value == "es"
|
||||
assert graphene_type._meta.enum.__members__["ES"].description == "Spanish"
|
||||
assert graphene_type._meta.enum.__members__["EN"].value == "en"
|
||||
assert graphene_type._meta.enum.__members__["EN"].description == "English"
|
||||
|
||||
|
||||
def test_field_with_grouped_choices():
|
||||
field = models.CharField(
|
||||
help_text="Language",
|
||||
|
|
Loading…
Reference in New Issue
Block a user