@@ -65,32 +65,58 @@ def construct_fields(
6565def validate_fields (type_ , model , fields , only_fields , exclude_fields ):
6666 # Validate the given fields against the model's fields and custom fields
6767 all_field_names = set (fields .keys ())
68- for fields_list in ( only_fields , exclude_fields ):
69- if not fields_list :
68+ for name in only_fields or ( ):
69+ if name in all_field_names :
7070 continue
71- for name in fields_list :
72- if name in all_field_names :
73- continue
7471
75- if hasattr (model , name ):
76- warnings .warn (
77- (
78- 'Field name "{field_name}" matches an attribute on Django model "{app_label}.{object_name}" '
79- "but it's not a model field so Graphene cannot determine what type it should be. "
80- 'Either define the type of the field on DjangoObjectType "{type_}" or remove it from the "fields" list.'
81- ).format (
82- field_name = name ,
83- app_label = model ._meta .app_label ,
84- object_name = model ._meta .object_name ,
85- type_ = type_ ,
86- )
72+ if hasattr (model , name ):
73+ warnings .warn (
74+ (
75+ 'Field name "{field_name}" matches an attribute on Django model "{app_label}.{object_name}" '
76+ "but it's not a model field so Graphene cannot determine what type it should be. "
77+ 'Either define the type of the field on DjangoObjectType "{type_}" or remove it from the "fields" list.'
78+ ).format (
79+ field_name = name ,
80+ app_label = model ._meta .app_label ,
81+ object_name = model ._meta .object_name ,
82+ type_ = type_ ,
8783 )
84+ )
8885
89- else :
86+ else :
87+ warnings .warn (
88+ (
89+ 'Field name "{field_name}" doesn\' t exist on Django model "{app_label}.{object_name}". '
90+ 'Consider removing the field from the "fields" list of DjangoObjectType "{type_}" because it has no effect.'
91+ ).format (
92+ field_name = name ,
93+ app_label = model ._meta .app_label ,
94+ object_name = model ._meta .object_name ,
95+ type_ = type_ ,
96+ )
97+ )
98+
99+ # Validate exclude fields
100+ for name in exclude_fields or ():
101+ if name in all_field_names :
102+ # Field is a custom field
103+ warnings .warn (
104+ (
105+ 'Excluding the custom field "{field_name} on DjangoObjectType "{type_}" has no effect. '
106+ 'Either remove the custom field or remove the field from the "exclude" list.'
107+ ).format (
108+ field_name = name ,
109+ app_label = model ._meta .app_label ,
110+ object_name = model ._meta .object_name ,
111+ type_ = type_ ,
112+ )
113+ )
114+ else :
115+ if not hasattr (model , name ):
90116 warnings .warn (
91117 (
92- 'Field name "{field_name}" doesn \' t exist on Django model "{app_label}.{object_name }". '
93- 'Consider removing the field from the "fields " list of DjangoObjectType "{type_}" because it has no effect. '
118+ 'Django model "{app_label}.{object_name}" does not have a field or attribute named "{field_name }". '
119+ 'Consider removing the field from the "exclude " list of DjangoObjectType "{type_}" because it has no effect'
94120 ).format (
95121 field_name = name ,
96122 app_label = model ._meta .app_label ,
0 commit comments