File tree Expand file tree Collapse file tree 3 files changed +19
-12
lines changed Expand file tree Collapse file tree 3 files changed +19
-12
lines changed Original file line number Diff line number Diff line change @@ -153,19 +153,20 @@ Default: ``False``
153
153
``DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME ``
154
154
--------------------------------------
155
155
156
- Set to a function that takes the Django choice field and returns a string to completely customise the naming for the Enum type.
156
+ 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.
157
157
158
158
If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING `` setting is ignored.
159
159
160
160
Default: ``None ``
161
161
162
162
.. code :: python
163
163
164
+ # myapp.utils
164
165
def enum_naming (field ):
165
166
if isinstance (field.model, User):
166
167
return f " CustomUserEnum { field.name.title()} "
167
168
return f " CustomEnum { field.name.title()} "
168
169
169
170
GRAPHENE = {
170
- ' DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME' : enum_naming
171
+ ' DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME' : " myapp.utils. enum_naming"
171
172
}
Original file line number Diff line number Diff line change 1
1
from collections import OrderedDict
2
2
from django .db import models
3
3
from django .utils .encoding import force_str
4
+ from django .utils .module_loading import import_string
4
5
5
6
from graphene import (
6
7
ID ,
@@ -70,10 +71,12 @@ def description(self):
70
71
71
72
72
73
def generate_enum_name (django_model_meta , field ):
73
- if graphene_settings .DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME and callable (
74
- graphene_settings .DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
75
- ):
76
- name = graphene_settings .DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME (field )
74
+ if graphene_settings .DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME :
75
+ # Try and import custom function
76
+ custom_func = import_string (
77
+ graphene_settings .DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
78
+ )
79
+ name = custom_func (field )
77
80
elif graphene_settings .DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True :
78
81
name = "{app_label}{object_name}{field_name}Choices" .format (
79
82
app_label = to_camel_case (django_model_meta .app_label .title ()),
Original file line number Diff line number Diff line change @@ -388,6 +388,10 @@ class Meta:
388
388
assert len (record ) == 0
389
389
390
390
391
+ def custom_enum_name (field ):
392
+ return "CustomEnum{}" .format (field .name .title ())
393
+
394
+
391
395
class TestDjangoObjectType :
392
396
@pytest .fixture
393
397
def PetModel (self ):
@@ -532,10 +536,9 @@ class Query(ObjectType):
532
536
graphene_settings .DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = False
533
537
534
538
def test_django_objecttype_choices_custom_enum_name (self , PetModel ):
535
- def custom_name (field ):
536
- return "CustomEnum{}" .format (field .name .title ())
537
-
538
- graphene_settings .DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = custom_name
539
+ graphene_settings .DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = (
540
+ "graphene_django.tests.test_types.custom_enum_name"
541
+ )
539
542
540
543
class PetModelKind (DjangoObjectType ):
541
544
class Meta :
@@ -553,14 +556,14 @@ class Query(ObjectType):
553
556
query: Query
554
557
}
555
558
556
- enum DjangoModelTestsPetModelKindChoices {
559
+ enum CustomEnumKind {
557
560
CAT
558
561
DOG
559
562
}
560
563
561
564
type PetModelKind {
562
565
id: ID!
563
- kind: DjangoModelTestsPetModelKindChoices !
566
+ kind: CustomEnumKind !
564
567
}
565
568
566
569
type Query {
You can’t perform that action at this time.
0 commit comments