mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 09:42:32 +03:00
Update documentation
This commit is contained in:
parent
e2ca1ed5e0
commit
2d71a107d5
|
@ -92,6 +92,66 @@ You can completely overwrite a field, or add new fields, to a ``DjangoObjectType
|
||||||
return 'hello!'
|
return 'hello!'
|
||||||
|
|
||||||
|
|
||||||
|
Choices to Enum conversion
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
By default Graphene-Django will convert any Django fields that have ``choices``
|
||||||
|
into a GraphQL enum type.
|
||||||
|
|
||||||
|
For example the following ``Model`` and ``DjangoObjectType``:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
class PetModel(models.Model):
|
||||||
|
kind = models.CharField(max_length=100, choices=(('cat', 'Cat'), ('dog', 'Dog')))
|
||||||
|
|
||||||
|
class Pet(DjangoObjectType):
|
||||||
|
class Meta:
|
||||||
|
model = PetModel
|
||||||
|
|
||||||
|
Results in the following GraphQL schema definition:
|
||||||
|
|
||||||
|
.. code::
|
||||||
|
|
||||||
|
type Pet {
|
||||||
|
id: ID!
|
||||||
|
kind: PetModelKind!
|
||||||
|
}
|
||||||
|
|
||||||
|
enum PetModelKind {
|
||||||
|
CAT
|
||||||
|
DOG
|
||||||
|
}
|
||||||
|
|
||||||
|
You can disable this automatic conversion by setting
|
||||||
|
``convert_choices_to_enum`` attribute to ``False`` on the ``DjangoObjectType``
|
||||||
|
``Meta`` class.
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
class Pet(DjangoObjectType):
|
||||||
|
class Meta:
|
||||||
|
model = PetModel
|
||||||
|
convert_choices_to_enum = False
|
||||||
|
|
||||||
|
.. code::
|
||||||
|
|
||||||
|
type Pet {
|
||||||
|
id: ID!
|
||||||
|
kind: String!
|
||||||
|
}
|
||||||
|
|
||||||
|
You can also set ``convert_choices_to_enum`` to a list of fields that should be
|
||||||
|
automatically converted into enums:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
class Pet(DjangoObjectType):
|
||||||
|
class Meta:
|
||||||
|
model = PetModel
|
||||||
|
convert_choices_to_enum = ['kind']
|
||||||
|
|
||||||
|
|
||||||
Related models
|
Related models
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user