Expand warning messages

This commit is contained in:
Jonathan Kim 2020-01-31 15:20:33 +00:00
parent 47650e760a
commit 948e501ad8
2 changed files with 23 additions and 17 deletions

View File

@ -327,19 +327,12 @@ def test_django_objecttype_fields_exclude_exist_on_model():
model = ReporterModel model = ReporterModel
fields = ["first_name", "foo", "email"] 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( with pytest.warns(
UserWarning, 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: class Meta:
model = ReporterModel model = ReporterModel
fields = ["first_name", "some_method", "email"] 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 # Don't warn if selecting a custom field
with pytest.warns(None) as record: with pytest.warns(None) as record:
class Reporter4(DjangoObjectType): class Reporter3(DjangoObjectType):
custom_field = String() custom_field = String()
class Meta: class Meta:

View File

@ -62,7 +62,7 @@ def construct_fields(
return 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 # Validate the given fields against the model's fields and custom fields
all_field_names = set(fields.keys()) all_field_names = set(fields.keys())
for fields_list in (only_fields, exclude_fields): 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): if hasattr(model, name):
warnings.warn( 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: else:
warnings.warn( 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
validate_fields(model, _meta.fields, fields, exclude) validate_fields(cls, model, _meta.fields, fields, exclude)
if not skip_registry: if not skip_registry:
registry.register(cls) registry.register(cls)