Reputation: 613
I am using this code for ordering , its working fine on case of Name, url etc But I want to order on basis of id (which is integer field) and I get this error
function lower(integer) does not exist LINE 1: ...ws_sources_newssource"."deleted" IS NULL ORDER BY LOWER("new...
And my code is as fellow
class CaseInsensitiveOrderingFilter(OrderingFilter):
def filter_queryset(self, request, queryset, view):
ordering = self.get_ordering(request, queryset, view)
if ordering:
new_ordering = []
for field in ordering:
if field.startswith('-'):
new_ordering.append(Lower(field[1:]).desc())
else:
new_ordering.append(Lower(field).asc())
return queryset.order_by(*new_ordering)
return queryset
then use it in the ListView
class IngredientListAPIView(ListAPIView):
search_fields = ['name']
queryset = models.NewsData.objects.all()
filter_backends =(filters.SearchFilter,CaseInsensitiveOrderingFilter,)
ordering_fields = ['id','name','url','language__name','count','key_journalists',]
ordering = ('name')
Upvotes: 0
Views: 133
Reputation: 19620
The error message is explicit, you cannot use function lower() on an integer. Sqlite does not really have types, so the data came back as string. Postgres does have types and field
is coming back as an integer. You will need to explicitly cast field
as str(field)
UPDATE If you want to sort by integer then something like:
if ordering:
return queryset.order_by(field)
Upvotes: 1