Skip to content

Allow DjangoObjectType to have default meta #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion graphene_django/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from graphene.relay import Node

from .. import registry
from ..types import DjangoObjectType
from ..types import DjangoObjectType, DjangoObjectTypeOptions
from .models import Article as ArticleModel
from .models import Reporter as ReporterModel

Expand Down Expand Up @@ -67,6 +67,26 @@ def test_django_objecttype_with_node_have_correct_fields():
assert list(fields.keys()) == ['id', 'headline', 'pub_date', 'reporter', 'editor', 'lang', 'importance']


def test_django_objecttype_with_custom_meta():
class ArticleTypeOptions(DjangoObjectTypeOptions):
'''Article Type Options'''

class ArticleType(DjangoObjectType):
class Meta:
abstract = True

@classmethod
def __init_subclass_with_meta__(cls, _meta=None, **options):
_meta = ArticleTypeOptions(cls)
super(ArticleType, cls).__init_subclass_with_meta__(_meta=_meta, **options)

class Article(ArticleType):
class Meta:
model = ArticleModel

assert isinstance(Article._meta, ArticleTypeOptions)


def test_schema_representation():
expected = """
schema {
Expand Down
6 changes: 4 additions & 2 deletions graphene_django/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DjangoObjectType(ObjectType):
@classmethod
def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=False,
only_fields=(), exclude_fields=(), filter_fields=None, connection=None,
connection_class=None, use_connection=None, interfaces=(), **options):
connection_class=None, use_connection=None, interfaces=(), _meta=None, **options):
assert is_valid_django_model(model), (
'You need to pass a valid Django Model in {}.Meta, received "{}".'
).format(cls.__name__, model)
Expand Down Expand Up @@ -82,7 +82,9 @@ def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=Fa
"The connection must be a Connection. Received {}"
).format(connection.__name__)

_meta = DjangoObjectTypeOptions(cls)
if not _meta:
_meta = DjangoObjectTypeOptions(cls)

_meta.model = model
_meta.registry = registry
_meta.filter_fields = filter_fields
Expand Down