Below is the RDBMS diagram of the Django/Graphene example I am working with:
Below is the code for my model.py for the app:
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)
Below is Schema.py for the app:
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()
Without the mutation codes, query request works fine as illustrated below
But it would output an error when mutation codes are added and I can't figure out what I did wrong as I am a noob. Please help!!
AssertionError: You need to pass a valid Django Model in CreateProductCategory.Meta, received "None".