mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-04-24 11:02:04 +03:00
Import function through import_string function
This commit is contained in:
parent
d0e3a04ac7
commit
9cff36573c
|
@ -153,7 +153,7 @@ Default: ``False``
|
|||
``DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME``
|
||||
--------------------------------------
|
||||
|
||||
Set to a function that takes the Django choice field and returns a string to completely customise the naming for the Enum type.
|
||||
Define the path of a function that takes the Django choice field and returns a string to completely customise the naming for the Enum type.
|
||||
|
||||
If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING`` setting is ignored.
|
||||
|
||||
|
@ -161,11 +161,12 @@ Default: ``None``
|
|||
|
||||
.. code:: python
|
||||
|
||||
# myapp.utils
|
||||
def enum_naming(field):
|
||||
if isinstance(field.model, User):
|
||||
return f"CustomUserEnum{field.name.title()}"
|
||||
return f"CustomEnum{field.name.title()}"
|
||||
|
||||
GRAPHENE = {
|
||||
'DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME': enum_naming
|
||||
'DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME': "myapp.utils.enum_naming"
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from collections import OrderedDict
|
||||
from django.db import models
|
||||
from django.utils.encoding import force_str
|
||||
from django.utils.module_loading import import_string
|
||||
|
||||
from graphene import (
|
||||
ID,
|
||||
|
@ -70,10 +71,12 @@ def convert_choices_to_named_enum_with_descriptions(name, choices):
|
|||
|
||||
|
||||
def generate_enum_name(django_model_meta, field):
|
||||
if graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME and callable(
|
||||
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
|
||||
):
|
||||
name = graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME(field)
|
||||
if graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME:
|
||||
# Try and import custom function
|
||||
custom_func = import_string(
|
||||
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
|
||||
)
|
||||
name = custom_func(field)
|
||||
elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True:
|
||||
name = "{app_label}{object_name}{field_name}Choices".format(
|
||||
app_label=to_camel_case(django_model_meta.app_label.title()),
|
||||
|
|
|
@ -388,6 +388,10 @@ def test_django_objecttype_exclude_fields_exist_on_model():
|
|||
assert len(record) == 0
|
||||
|
||||
|
||||
def custom_enum_name(field):
|
||||
return "CustomEnum{}".format(field.name.title())
|
||||
|
||||
|
||||
class TestDjangoObjectType:
|
||||
@pytest.fixture
|
||||
def PetModel(self):
|
||||
|
@ -532,10 +536,9 @@ class TestDjangoObjectType:
|
|||
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = False
|
||||
|
||||
def test_django_objecttype_choices_custom_enum_name(self, PetModel):
|
||||
def custom_name(field):
|
||||
return "CustomEnum{}".format(field.name.title())
|
||||
|
||||
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = custom_name
|
||||
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = (
|
||||
"graphene_django.tests.test_types.custom_enum_name"
|
||||
)
|
||||
|
||||
class PetModelKind(DjangoObjectType):
|
||||
class Meta:
|
||||
|
@ -553,14 +556,14 @@ class TestDjangoObjectType:
|
|||
query: Query
|
||||
}
|
||||
|
||||
enum DjangoModelTestsPetModelKindChoices {
|
||||
enum CustomEnumKind {
|
||||
CAT
|
||||
DOG
|
||||
}
|
||||
|
||||
type PetModelKind {
|
||||
id: ID!
|
||||
kind: DjangoModelTestsPetModelKindChoices!
|
||||
kind: CustomEnumKind!
|
||||
}
|
||||
|
||||
type Query {
|
||||
|
|
Loading…
Reference in New Issue
Block a user