Skip to content

Commit c7f8b14

Browse files
Filter model properties in get_default_valid_fields of OrderingFilter
1 parent 62113a7 commit c7f8b14

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

rest_framework/filters.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,19 @@ def get_default_valid_fields(self, queryset, view, context={}):
226226
)
227227
raise ImproperlyConfigured(msg % self.__class__.__name__)
228228

229-
model_field_names = [field.name for field in queryset.model._meta.fields]
229+
model_class = queryset.model
230+
model_property_names = [
231+
# 'pk' is a property added in Django's Model class, however it is valid for ordering.
232+
attr for attr in dir(model_class) if isinstance(getattr(model_class, attr), property) and attr != 'pk'
233+
]
230234

231235
return [
232236
(field.source.replace('.', '__') or field_name, field.label)
233237
for field_name, field in serializer_class(context=context).fields.items()
234238
if (
235239
not getattr(field, 'write_only', False) and
236-
not field.source == '*' and (
237-
field_name in model_field_names or field.source in model_field_names
238-
)
240+
not field.source == '*' and
241+
field.source not in model_property_names
239242
)
240243
]
241244

0 commit comments

Comments
 (0)