Merge pull request #379 from mongkok/default-meta

Allow DjangoObjectType to have default meta
This commit is contained in:
Syrus Akbary 2018-02-02 15:06:09 -08:00 committed by GitHub
commit e827b10e7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -4,7 +4,7 @@ from graphene import Interface, ObjectType, Schema, Connection, String
from graphene.relay import Node from graphene.relay import Node
from .. import registry from .. import registry
from ..types import DjangoObjectType from ..types import DjangoObjectType, DjangoObjectTypeOptions
from .models import Article as ArticleModel from .models import Article as ArticleModel
from .models import Reporter as ReporterModel from .models import Reporter as ReporterModel
@ -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'] 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(): def test_schema_representation():
expected = """ expected = """
schema { schema {

View File

@ -45,7 +45,7 @@ class DjangoObjectType(ObjectType):
@classmethod @classmethod
def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=False, def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=False,
only_fields=(), exclude_fields=(), filter_fields=None, connection=None, 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), ( assert is_valid_django_model(model), (
'You need to pass a valid Django Model in {}.Meta, received "{}".' 'You need to pass a valid Django Model in {}.Meta, received "{}".'
).format(cls.__name__, model) ).format(cls.__name__, model)
@ -82,7 +82,9 @@ class DjangoObjectType(ObjectType):
"The connection must be a Connection. Received {}" "The connection must be a Connection. Received {}"
).format(connection.__name__) ).format(connection.__name__)
if not _meta:
_meta = DjangoObjectTypeOptions(cls) _meta = DjangoObjectTypeOptions(cls)
_meta.model = model _meta.model = model
_meta.registry = registry _meta.registry = registry
_meta.filter_fields = filter_fields _meta.filter_fields = filter_fields