Skip to content

Commit

Permalink
Merge pull request #379 from mongkok/default-meta
Browse files Browse the repository at this point in the history
Allow DjangoObjectType to have default meta
  • Loading branch information
syrusakbary authored Feb 2, 2018
2 parents fd3faf0 + 3051374 commit e827b10
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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

0 comments on commit e827b10

Please sign in to comment.