Django/Graphene 突变错误 "you need to pass a valid Django model" 无法修复
Django/Graphene Mutation error "you need to pass a valid Django model" can't fix it
下面是我正在使用的 Django/Graphene 示例的 RDBMS 图:
下面是我的 model.py 应用代码:
from django.db import models
# Create your models here.
class ProductCategory(models.Model):
category = models.CharField(max_length=50)
parentCategory = models.ForeignKey('self',null=True, on_delete=models.CASCADE)
class Product(models.Model):
productNumber= models.CharField(max_length=50)
description = models.CharField(max_length=50)
productCategory= models.ForeignKey('product.ProductCategory', on_delete=models.PROTECT)
以下是应用的 Schema.py:
import graphene
from graphene_django import DjangoObjectType
from .models import Product, ProductCategory
class ProductType(DjangoObjectType):
class Meta:
model = Product
class ProductCategoryType(DjangoObjectType):
class Meta:
model = ProductCategory
class Query(graphene.ObjectType):
products = graphene.List(ProductType)
productCategories = graphene.List(ProductCategoryType)
def resolve_products(self, info):
return Product.objects.all()
def resolve_productCategories(self,info):
return ProductCategory.objects.all()
class CreateProductCategory(DjangoObjectType):
productCategory = graphene.Field(ProductCategoryType)
class Arguments:
category = graphene.String(required=True)
parentCategory = graphene.Int()
def mutate(self, info, category, parentCategory):
productCategory = ProductCategory(category = category, parentCategory = parentCategory)
productCategory.save()
return CreateProductCategory(productCategory=productCategory)
return CreateProductCategory(category=category,parentCategory=parentCategory)
class Mutation(graphene.ObjectType):
createProductCategory= CreateProductCategory.Field()
没有突变代码,查询请求工作正常,如下图所示
但是加了突变码会报错,我是菜鸟也搞不懂自己做错了什么。请帮忙!!
AssertionError:您需要在 CreateProductCategory.Meta 中传递有效的 Django 模型,收到“None”。
问题是:class CreateProductCategory(DjangoObjectType)
您需要继承 graphene.Mutation
,而不是 DjangoObjectType
,才能进行突变。
正确:class CreateProductCategory(graphene.Mutation)
`
下面是我正在使用的 Django/Graphene 示例的 RDBMS 图:
下面是我的 model.py 应用代码:
from django.db import models
# Create your models here.
class ProductCategory(models.Model):
category = models.CharField(max_length=50)
parentCategory = models.ForeignKey('self',null=True, on_delete=models.CASCADE)
class Product(models.Model):
productNumber= models.CharField(max_length=50)
description = models.CharField(max_length=50)
productCategory= models.ForeignKey('product.ProductCategory', on_delete=models.PROTECT)
以下是应用的 Schema.py:
import graphene
from graphene_django import DjangoObjectType
from .models import Product, ProductCategory
class ProductType(DjangoObjectType):
class Meta:
model = Product
class ProductCategoryType(DjangoObjectType):
class Meta:
model = ProductCategory
class Query(graphene.ObjectType):
products = graphene.List(ProductType)
productCategories = graphene.List(ProductCategoryType)
def resolve_products(self, info):
return Product.objects.all()
def resolve_productCategories(self,info):
return ProductCategory.objects.all()
class CreateProductCategory(DjangoObjectType):
productCategory = graphene.Field(ProductCategoryType)
class Arguments:
category = graphene.String(required=True)
parentCategory = graphene.Int()
def mutate(self, info, category, parentCategory):
productCategory = ProductCategory(category = category, parentCategory = parentCategory)
productCategory.save()
return CreateProductCategory(productCategory=productCategory)
return CreateProductCategory(category=category,parentCategory=parentCategory)
class Mutation(graphene.ObjectType):
createProductCategory= CreateProductCategory.Field()
没有突变代码,查询请求工作正常,如下图所示
但是加了突变码会报错,我是菜鸟也搞不懂自己做错了什么。请帮忙!!
AssertionError:您需要在 CreateProductCategory.Meta 中传递有效的 Django 模型,收到“None”。
问题是:class CreateProductCategory(DjangoObjectType)
您需要继承 graphene.Mutation
,而不是 DjangoObjectType
,才能进行突变。
正确:class CreateProductCategory(graphene.Mutation)
`