Django/石墨烯突变错误“;您需要传递一个有效的Django模型;can';我修不好
下面是我正在使用的Django/石墨烯示例的RDBMS图: 以下是应用程序my model.py的代码:Django/石墨烯突变错误“;您需要传递一个有效的Django模型;can';我修不好,django,graphene-django,Django,Graphene Django,下面是我正在使用的Django/石墨烯示例的RDBMS图: 以下是应用程序my 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
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)
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()
下面是应用程序的Schema.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)
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()
如果没有变异代码,查询请求可以正常工作,如下所示
但当添加变异代码时,它会输出一个错误,我无法找出我做错了什么,因为我是一个noob。请帮忙
AssertionError:您需要在CreateProductCategory.Meta中传递一个有效的Django模型,收到“无”。这就是问题所在:
类CreateProductCategory(DjangObjectType)
对于突变,您需要继承graphene.Mutation
,而不是djangObjectType
正确:class CreateProductCategory(graphene.Mutation)
`这是否回答了您的问题?谢谢它就像一个符咒。