Update docs to take new warning into account

* all examples have fields or exclude set
* mention that if neither fields nor exclude is used, warning will be raised
This commit is contained in:
radoslaw.kowalski 2020-06-07 12:34:50 +02:00
parent d478275bca
commit 3d497ea094
7 changed files with 25 additions and 0 deletions

View File

@ -111,6 +111,7 @@ If you are using ``DjangoObjectType`` you can define a custom `get_queryset`.
class PostNode(DjangoObjectType): class PostNode(DjangoObjectType):
class Meta: class Meta:
model = Post model = Post
fields = '__all__'
@classmethod @classmethod
def get_queryset(cls, queryset, info): def get_queryset(cls, queryset, info):

View File

@ -45,6 +45,7 @@ For example:
class Meta: class Meta:
# Assume you have an Animal model defined with the following fields # Assume you have an Animal model defined with the following fields
model = Animal model = Animal
fields = '__all__'
filter_fields = ['name', 'genus', 'is_domesticated'] filter_fields = ['name', 'genus', 'is_domesticated']
interfaces = (relay.Node, ) interfaces = (relay.Node, )
@ -75,6 +76,7 @@ You can also make more complex lookup types available:
class AnimalNode(DjangoObjectType): class AnimalNode(DjangoObjectType):
class Meta: class Meta:
model = Animal model = Animal
fields = '__all__'
# Provide more complex lookup types # Provide more complex lookup types
filter_fields = { filter_fields = {
'name': ['exact', 'icontains', 'istartswith'], 'name': ['exact', 'icontains', 'istartswith'],
@ -116,6 +118,7 @@ create your own ``FilterSet``. You can pass it directly as follows:
class Meta: class Meta:
# Assume you have an Animal model defined with the following fields # Assume you have an Animal model defined with the following fields
model = Animal model = Animal
fields = '__all__'
filter_fields = ['name', 'genus', 'is_domesticated'] filter_fields = ['name', 'genus', 'is_domesticated']
interfaces = (relay.Node, ) interfaces = (relay.Node, )
@ -179,6 +182,7 @@ in unison with the ``filter_fields`` parameter:
class AnimalNode(DjangoObjectType): class AnimalNode(DjangoObjectType):
class Meta: class Meta:
model = Animal model = Animal
fields = '__all__'
filterset_class = AnimalFilter filterset_class = AnimalFilter
interfaces = (relay.Node, ) interfaces = (relay.Node, )
@ -236,6 +240,7 @@ Extend the tuple of fields if you want to order by more than one field.
class Meta: class Meta:
name = 'Group' name = 'Group'
model = GroupModel model = GroupModel
fields = '__all__'
interfaces = (relay.Node,) interfaces = (relay.Node,)
def resolve_users(self, info, **kwargs): def resolve_users(self, info, **kwargs):

View File

@ -25,6 +25,7 @@ Simple example
class QuestionType(DjangoObjectType): class QuestionType(DjangoObjectType):
class Meta: class Meta:
model = Question model = Question
fields = '__all__'
class QuestionMutation(graphene.Mutation): class QuestionMutation(graphene.Mutation):
@ -90,6 +91,7 @@ DjangoModelFormMutation
class PetType(DjangoObjectType): class PetType(DjangoObjectType):
class Meta: class Meta:
model = Pet model = Pet
fields = '__all__'
class PetMutation(DjangoModelFormMutation): class PetMutation(DjangoModelFormMutation):
pet = Field(PetType) pet = Field(PetType)

View File

@ -28,6 +28,7 @@ Full example
class QuestionType(DjangoObjectType): class QuestionType(DjangoObjectType):
class Meta: class Meta:
model = Question model = Question
fields = '__all__'
class Query: class Query:
@ -53,6 +54,10 @@ all fields that should be exposed using the fields attribute.
This will make it less likely to result in unintentionally exposing data when This will make it less likely to result in unintentionally exposing data when
your models change. your models change.
Setting neither ``fields`` nor ``exclude`` is allowed but will raise a warning, to
avoid that you can easily make ``DjangoObjectType`` include all fields in the model as
described below.
``fields`` ``fields``
~~~~~~~~~~ ~~~~~~~~~~
@ -127,6 +132,7 @@ For example the following ``Model`` and ``DjangoObjectType``:
class Pet(DjangoObjectType): class Pet(DjangoObjectType):
class Meta: class Meta:
model = PetModel model = PetModel
fields = '__all__'
Results in the following GraphQL schema definition: Results in the following GraphQL schema definition:
@ -151,6 +157,7 @@ You can disable this automatic conversion by setting
class Pet(DjangoObjectType): class Pet(DjangoObjectType):
class Meta: class Meta:
model = PetModel model = PetModel
fields = '__all__'
convert_choices_to_enum = False convert_choices_to_enum = False
.. code:: .. code::
@ -168,6 +175,7 @@ automatically converted into enums:
class Pet(DjangoObjectType): class Pet(DjangoObjectType):
class Meta: class Meta:
model = PetModel model = PetModel
fields = '__all__'
convert_choices_to_enum = ['kind'] convert_choices_to_enum = ['kind']
**Note:** Setting ``convert_choices_to_enum = []`` is the same as setting it to **Note:** Setting ``convert_choices_to_enum = []`` is the same as setting it to
@ -206,6 +214,7 @@ need to create the most basic class for this to work:
class CategoryType(DjangoObjectType): class CategoryType(DjangoObjectType):
class Meta: class Meta:
model = Category model = Category
fields = '__all__'
.. _django-objecttype-get-queryset: .. _django-objecttype-get-queryset:
@ -224,6 +233,7 @@ Use this to control filtering on the ObjectType level instead of the Query objec
class QuestionType(DjangoObjectType): class QuestionType(DjangoObjectType):
class Meta: class Meta:
model = Question model = Question
fields = '__all__'
@classmethod @classmethod
def get_queryset(cls, queryset, info): def get_queryset(cls, queryset, info):
@ -347,6 +357,7 @@ the core graphene pages for more information on customizing the Relay experience
class QuestionType(DjangoObjectType): class QuestionType(DjangoObjectType):
class Meta: class Meta:
model = Question model = Question
fields = '__all__'
interfaces = (relay.Node,) interfaces = (relay.Node,)

View File

@ -8,11 +8,13 @@
class CategoryType(DjangoObjectType): class CategoryType(DjangoObjectType):
class Meta: class Meta:
model = Category model = Category
fields = '__all__'
class IngredientType(DjangoObjectType): class IngredientType(DjangoObjectType):
class Meta: class Meta:
model = Ingredient model = Ingredient
fields = '__all__'
class Query(object): class Query(object):

View File

@ -157,11 +157,13 @@ Create ``cookbook/ingredients/schema.py`` and type the following:
class CategoryType(DjangoObjectType): class CategoryType(DjangoObjectType):
class Meta: class Meta:
model = Category model = Category
fields = '__all__'
class IngredientType(DjangoObjectType): class IngredientType(DjangoObjectType):
class Meta: class Meta:
model = Ingredient model = Ingredient
fields = '__all__'
class Query(object): class Query(object):

View File

@ -130,6 +130,7 @@ Create ``cookbook/ingredients/schema.py`` and type the following:
class CategoryNode(DjangoObjectType): class CategoryNode(DjangoObjectType):
class Meta: class Meta:
model = Category model = Category
fields = '__all__'
filter_fields = ['name', 'ingredients'] filter_fields = ['name', 'ingredients']
interfaces = (relay.Node, ) interfaces = (relay.Node, )
@ -137,6 +138,7 @@ Create ``cookbook/ingredients/schema.py`` and type the following:
class IngredientNode(DjangoObjectType): class IngredientNode(DjangoObjectType):
class Meta: class Meta:
model = Ingredient model = Ingredient
fields = '__all__'
# Allow for some more advanced filtering here # Allow for some more advanced filtering here
filter_fields = { filter_fields = {
'name': ['exact', 'icontains', 'istartswith'], 'name': ['exact', 'icontains', 'istartswith'],