mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-12 09:12:18 +03:00
Warn when DjangoObjectType has neither fields nor exclude option
This commit is contained in:
parent
d9c187ffc2
commit
30306e8108
|
@ -476,6 +476,37 @@ def test_django_objecttype_exclude_fields_exist_on_model():
|
||||||
assert len(record) == 0
|
assert len(record) == 0
|
||||||
|
|
||||||
|
|
||||||
|
@with_local_registry
|
||||||
|
def test_django_objecttype_neither_fields_nor_exclude():
|
||||||
|
with pytest.warns(
|
||||||
|
PendingDeprecationWarning,
|
||||||
|
match=r"Creating a DjangoObjectType without either the `fields` "
|
||||||
|
"or the `exclude` option is deprecated.",
|
||||||
|
):
|
||||||
|
|
||||||
|
class Reporter(DjangoObjectType):
|
||||||
|
class Meta:
|
||||||
|
model = ReporterModel
|
||||||
|
|
||||||
|
with pytest.warns(None) as record:
|
||||||
|
|
||||||
|
class Reporter2(DjangoObjectType):
|
||||||
|
class Meta:
|
||||||
|
model = ReporterModel
|
||||||
|
fields = ["email"]
|
||||||
|
|
||||||
|
assert len(record) == 0
|
||||||
|
|
||||||
|
with pytest.warns(None) as record:
|
||||||
|
|
||||||
|
class Reporter3(DjangoObjectType):
|
||||||
|
class Meta:
|
||||||
|
model = ReporterModel
|
||||||
|
exclude = ["email"]
|
||||||
|
|
||||||
|
assert len(record) == 0
|
||||||
|
|
||||||
|
|
||||||
def custom_enum_name(field):
|
def custom_enum_name(field):
|
||||||
return "CustomEnum{}".format(field.name.title())
|
return "CustomEnum{}".format(field.name.title())
|
||||||
|
|
||||||
|
|
|
@ -220,6 +220,16 @@ class DjangoObjectType(ObjectType):
|
||||||
% type(exclude).__name__
|
% type(exclude).__name__
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if fields is None and exclude is None:
|
||||||
|
warnings.warn(
|
||||||
|
"Creating a DjangoObjectType without either the `fields` "
|
||||||
|
"or the `exclude` option is deprecated. Add an explicit `fields "
|
||||||
|
"= '__all__'` option on DjangoObjectType {class_name} to use all "
|
||||||
|
"fields".format(class_name=cls.__name__,),
|
||||||
|
PendingDeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
|
||||||
django_fields = yank_fields_from_attrs(
|
django_fields = yank_fields_from_attrs(
|
||||||
construct_fields(model, registry, fields, exclude, convert_choices_to_enum),
|
construct_fields(model, registry, fields, exclude, convert_choices_to_enum),
|
||||||
_as=Field,
|
_as=Field,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user