ProgrammingError: function lower(bigint) does not exist (Django, AWS RDS, PostgreSQL)
ProgrammingError: function lower(bigint) does not exist (Django, AWS RDS, PostgreSQL)
在我的 Django Rest Framework 项目中,我有一个自定义 filter_backends 允许不区分大小写进行过滤:
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:
# field = str(field)
print(Lower(field))
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
这在开发中工作正常。
现在我在 elastic beanstalk 上托管了 django 应用程序,并通过亚马逊关系数据库服务 (RDS) 配置了一个 postgresql 数据库。
当我现在尝试调用 API 时,出现此错误:
ProgrammingError at /api/profile_list/ function lower(bigint) does not
exist LINE 1: ..."."author_id") GROUP BY "user_user"."id" ORDER BY
LOWER(COUN...
HINT: No function matches the given name and argument types. You
might need to add explicit type casts.
此错误仅出现在 RDS 部署中。
我尝试在 django 中输入强制转换字段:
field = str(field)
但这不起作用。有什么方法可以在没有 lower 函数的情况下允许不区分大小写的排序,或者我如何有条件地检查它是一个数字(然后转换?)或文本 abd
您收到错误是因为您在可能是 IntegerField
或其他内容的字段上使用了 Lower
。您想在实际使用 Lower
:
之前检查您的字段是什么
from django.db import models
def get_field_type(field_name, queryset):
stripped_field_name = field_name.lstrip('-')
if stripped_field_name in queryset.query.annotations:
return queryset.query.annotations[stripped_field_name].output_field
return queryset.model._meta.get_field(stripped_field_name)
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 not isinstance(get_field_type(field, queryset), (models.CharField, models.TextField)):
# Most of the character type fields inherit from CharField.
# I might miss a few fields here so you would need to make sure
new_ordering.append(field)
elif field.startswith('-'):
new_ordering.append(Lower(field[1:]).desc())
else:
new_ordering.append(Lower(field).asc())
return queryset.order_by(*new_ordering)
return queryset
在我的 Django Rest Framework 项目中,我有一个自定义 filter_backends 允许不区分大小写进行过滤:
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:
# field = str(field)
print(Lower(field))
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
这在开发中工作正常。
现在我在 elastic beanstalk 上托管了 django 应用程序,并通过亚马逊关系数据库服务 (RDS) 配置了一个 postgresql 数据库。
当我现在尝试调用 API 时,出现此错误:
ProgrammingError at /api/profile_list/ function lower(bigint) does not exist LINE 1: ..."."author_id") GROUP BY "user_user"."id" ORDER BY LOWER(COUN...
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
此错误仅出现在 RDS 部署中。
我尝试在 django 中输入强制转换字段:
field = str(field)
但这不起作用。有什么方法可以在没有 lower 函数的情况下允许不区分大小写的排序,或者我如何有条件地检查它是一个数字(然后转换?)或文本 abd
您收到错误是因为您在可能是 IntegerField
或其他内容的字段上使用了 Lower
。您想在实际使用 Lower
:
from django.db import models
def get_field_type(field_name, queryset):
stripped_field_name = field_name.lstrip('-')
if stripped_field_name in queryset.query.annotations:
return queryset.query.annotations[stripped_field_name].output_field
return queryset.model._meta.get_field(stripped_field_name)
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 not isinstance(get_field_type(field, queryset), (models.CharField, models.TextField)):
# Most of the character type fields inherit from CharField.
# I might miss a few fields here so you would need to make sure
new_ordering.append(field)
elif field.startswith('-'):
new_ordering.append(Lower(field[1:]).desc())
else:
new_ordering.append(Lower(field).asc())
return queryset.order_by(*new_ordering)
return queryset