Skip to content

AttributeError: 'Options' object has no attribute 'name'

Published: at 09:23 AM

Symptoms

Faulty code:

import graphene
from graphene_django import DjangoObjectType
from e2ecov.core.fastory.fastory_fetch import get_all_fastory_data
from e2ecov.models import FastoryDataFile


class FastoryData(DjangoObjectType):
    class Meta:
        model = FastoryDataFile


class FastoryQuery(graphene.ObjectType):
    latest_fastory_data = graphene.Field(FastoryDataFile)

    def resolve_latest_fastory_data(self, info, **kwargs):
        result = get_all_fastory_data()
        if type(result) is str:
            fastory_data = FastoryData.objects.latest('createdAt')
            return fastory_data

        return result

When splitting the main schema into files, this errors occurred when opening graphql editor:

ImportError at /graphql
Could not import 'e2ecov.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: 'Options' object has no attribute 'name'.
Request Method:	GET
Request URL:	http://localhost:8000/graphql
Django Version:	3.1.3
Exception Type:	ImportError
Exception Value:
Could not import 'e2ecov.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: 'Options' object has no attribute 'name'.
Exception Location:	/home/jakub/.local/share/virtualenvs/backend-knk-YhIz/lib/python3.8/site-packages/graphene_django/settings.py, line 88, in import_from_string
Python Executable:	/home/jakub/.local/share/virtualenvs/backend-knk-YhIz/bin/python
Python Version:	3.8.6
Python Path:
['/home/jakub/dev/e2eserv/backend/server',
'/home/jakub/de/e2eserv/backend',
'/home/jakub/dev/e2eserv/backend/server',
'/opt/pycharm-professional/helpers/pycharm_display',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/jakub/.local/share/virtualenvs/backend-knk-YhIz/lib/python3.8/site-packages',
'/opt/pycharm-professional/helpers/pycharm_matplotlib_backend']
Server time:	Tue, 12 Jan 2021 08:48:51 +0000

Fix

RC was wrong model in query, the field should be of type FastoryData and not FastoryDataFile because FastoryData extends the DjangoObjectType

import graphene
from graphene_django import DjangoObjectType
from e2ecov.core.fastory.fastory_fetch import get_all_fastory_data
from e2ecov.models import FastoryDataFile


class FastoryData(DjangoObjectType):
    class Meta:
        model = FastoryDataFile


class FastoryQuery(graphene.ObjectType):
    latest_fastory_data = graphene.Field(FastoryData)

    def resolve_latest_fastory_data(self, info, **kwargs):
        result = get_all_fastory_data()
        if type(result) is str:
            fastory_data = FastoryData.objects.latest('createdAt')
            return fastory_data

        return result

Sources