mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 01:32:24 +03:00
Expand warning messages
This commit is contained in:
parent
47650e760a
commit
948e501ad8
|
@ -327,19 +327,12 @@ def test_django_objecttype_fields_exclude_exist_on_model():
|
|||
model = ReporterModel
|
||||
fields = ["first_name", "foo", "email"]
|
||||
|
||||
with pytest.warns(UserWarning, match=r"Field name .* doesn't exist"):
|
||||
|
||||
class Reporter2(DjangoObjectType):
|
||||
class Meta:
|
||||
model = ReporterModel
|
||||
exclude = ["first_name", "foo", "email"]
|
||||
|
||||
with pytest.warns(
|
||||
UserWarning,
|
||||
match=r"Field name .* exists on Django model .* but it's not a model field",
|
||||
):
|
||||
match=r"Field name .* matches an attribute on Django model .* but it's not a model field",
|
||||
) as record:
|
||||
|
||||
class Reporter3(DjangoObjectType):
|
||||
class Reporter2(DjangoObjectType):
|
||||
class Meta:
|
||||
model = ReporterModel
|
||||
fields = ["first_name", "some_method", "email"]
|
||||
|
@ -347,7 +340,7 @@ def test_django_objecttype_fields_exclude_exist_on_model():
|
|||
# Don't warn if selecting a custom field
|
||||
with pytest.warns(None) as record:
|
||||
|
||||
class Reporter4(DjangoObjectType):
|
||||
class Reporter3(DjangoObjectType):
|
||||
custom_field = String()
|
||||
|
||||
class Meta:
|
||||
|
|
|
@ -62,7 +62,7 @@ def construct_fields(
|
|||
return fields
|
||||
|
||||
|
||||
def validate_fields(model, fields, only_fields, exclude_fields):
|
||||
def validate_fields(type_, model, fields, only_fields, exclude_fields):
|
||||
# Validate the given fields against the model's fields and custom fields
|
||||
all_field_names = set(fields.keys())
|
||||
for fields_list in (only_fields, exclude_fields):
|
||||
|
@ -74,15 +74,28 @@ def validate_fields(model, fields, only_fields, exclude_fields):
|
|||
|
||||
if hasattr(model, name):
|
||||
warnings.warn(
|
||||
'Field name "{}" exists on Django model {} but it\'s not a model field.'.format(
|
||||
name, model
|
||||
(
|
||||
'Field name "{field_name}" matches an attribute on Django model "{app_label}.{object_name}" '
|
||||
"but it's not a model field so Graphene cannot determine what type it should be. "
|
||||
'Either define the type of the field on DjangoObjectType "{type_}" or remove it from the "fields" list.'
|
||||
).format(
|
||||
field_name=name,
|
||||
app_label=model._meta.app_label,
|
||||
object_name=model._meta.object_name,
|
||||
type_=type_,
|
||||
)
|
||||
)
|
||||
|
||||
else:
|
||||
warnings.warn(
|
||||
'Field name "{}" doesn\'t exist on Django model {}.'.format(
|
||||
name, model
|
||||
(
|
||||
'Field name "{field_name}" doesn\'t exist on Django model "{app_label}.{object_name}". '
|
||||
'Consider removing the field from the "fields" list of DjangoObjectType "{type_}" because it has no effect.'
|
||||
).format(
|
||||
field_name=name,
|
||||
app_label=model._meta.app_label,
|
||||
object_name=model._meta.object_name,
|
||||
type_=type_,
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -219,7 +232,7 @@ class DjangoObjectType(ObjectType):
|
|||
)
|
||||
|
||||
# Validate fields
|
||||
validate_fields(model, _meta.fields, fields, exclude)
|
||||
validate_fields(cls, model, _meta.fields, fields, exclude)
|
||||
|
||||
if not skip_registry:
|
||||
registry.register(cls)
|
||||
|
|
Loading…
Reference in New Issue
Block a user