From 948e501ad8002b7a72992f651ae5fa17d3ac2309 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Fri, 31 Jan 2020 15:20:33 +0000 Subject: [PATCH] Expand warning messages --- graphene_django/tests/test_types.py | 15 ++++----------- graphene_django/types.py | 25 +++++++++++++++++++------ 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index dcfabc4..a25383f 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -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: diff --git a/graphene_django/types.py b/graphene_django/types.py index 1d2af71..129dbe1 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -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)