Graphene-django: Adding DjangoModelFormMutation to schema raises import error

Created on 26 Mar 2019  路  5Comments  路  Source: graphql-python/graphene-django

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

question

Most helpful comment

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 :)

All 5 comments

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
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dawidpol picture Dawidpol  路  4Comments

licx picture licx  路  3Comments

x9sheikh picture x9sheikh  路  4Comments

dan-klasson picture dan-klasson  路  4Comments

MilanRgm picture MilanRgm  路  3Comments