[Django]-Django-filter order_by "Cannot resolve keyword 'name' into field"

2👍

Per the documentation on Meta.order_by, you have to use a field name from the model, not a filter field name. In your case, I would just use full_name, which is on the model:

class MyFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(name='full_name')

    class Meta:
        model = MyModel
        fields = ['name',]
        order_by_field = 'order'
        order_by = ('full_name',)

If you have a look at the code: https://github.com/alex/django-filter/blob/develop/django_filters/filterset.py#L326-L339, you will see that if you have declared an order_by field, it loops through the form fields (generated from the model), then uses that value as an argument to order_by on the QuerySet.

If anything, the documentation should be more explicit on this fact.

Leave a comment