Hi there!
I try to use the awesome DjangoModelFormMutation but when I add the mutation to the schema I always get this error:
Could not import 'project.config.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: 'NoneType' object has no attribute '_meta'.
Here's my setup:
class Pet(models.Model):
name = models.CharField()
class PetForm(forms.ModelForm):
class Meta:
model = Pet
fields = ('name',)
class PetMutation(DjangoModelFormMutation):
class Meta:
form_class = PetForm
class PetMutations(graphene.ObjectType):
pet = PetMutation.Field(description="Pets!")
class Mutation(PetMutations,
graphene.ObjectType):
pass
schema = graphene.Schema(query=Query, mutation=Mutation)
Any ideas what I might be doing wrong?
Thanks!
Ron
Ok, this seems to be a bug! When I copy all my types above my mutation class, I don't get this error. Took me about 5h to find this out... 馃槺
I added the types manually to the schema like this:
types = [PetType]
schema = graphene.Schema(query=Query, types=types, mutation=Mutation)
But no effect.
django-graphene is trying to import your schema file and is encountering another error along the way. I would recommend doing a manage.py shell and attempt manually importing the schema. This way you can see the import error without other code eating the message.
Ok, now I found the issue!
I need to import above my mutation the PetType! Without a usage...
from project.schemes.schematypes import PetType
class PetCrudMutation(DjangoModelFormMutation):
class Meta:
form_class = PetForm
class PetMutation(graphene.ObjectType):
pets= PetCrudMutation.Field("pets")
So this is really a bug.
Aaaaaaaaaaaaaaaaad I found the solution:
class PetCrudMutation(DjangoModelFormMutation):
pet= graphene.Field(PetType)
class Meta:
form_class = PetForm
I'm going to add this to the documentation :)
For me, this was occurring when I created a CreatePersonMutation without having created a PersonType:
class CreatePersonMutation(DjangoModelFormMutation):
class Meta:
form_class = PersonForm
ok = graphene.Boolean(required=True)
I was able to fix it just be defining a PersonType above the mutation:
class PersonType(DjangoObjectType):
class Meta:
model = Person
Most helpful comment
Aaaaaaaaaaaaaaaaad I found the solution:
I'm going to add this to the documentation :)