Skip to content

Add options to override how Django Choice fields are converted to Enums #860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 13, 2020
Prev Previous commit
Next Next commit
Add documentation
  • Loading branch information
jkimbo committed Feb 23, 2020
commit cad37176a683a7e82807960aa253836342bf93d5
29 changes: 29 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,32 @@ Default: ``False``
# 'messages': ['This field is required.'],
# }
# ]


``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING``
--------------------------------------

Set to ``True`` to use the new naming format for the auto generated Enum types from Django choice fields. The new format looks like this: ``DjangoModel{app_label}{object_name}{field_name}Choices``

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.

If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING`` setting is ignored.

Default: ``None``

.. code:: python

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
}