我的DRF序列化器具有以下代码:
unsubscribe
我的查询代码如下:
class ObjectSerializer(serializers.ModelSerializer):
class Meta:
model = models.MyModel
fields = ('field1', 'field2')
field1是一个带有尾随空格的char字段。我正在尝试删除查询集中的所有尾随空格。我该怎么做呢?我尝试将class MyAPI(ListAPIView):
http_method_names = ['get']
serializer_class = ObjectSerializer
def get_queryset(self):
queryset = MyModel.objects.values('field1', 'field2').filter(someField='SomeValue').all()
return queryset
语句传递到序列化器中,但它不会修剪字段上的空白。我做错的事很明显并且我只是想念吗?
编辑:这是模型代码的样子:
extra_kwargs = {"content": {"trim_whitespace": True}}
答案 0 :(得分:0)
与{strong> .annotate()
Trim()
数据库功能
from django.db.models.functions import Trim
class MyAPI(ListAPIView):
http_method_names = ['get']
serializer_class = ObjectSerializer
def get_queryset(self):
queryset = MyModel.objects.annotate(
field1_new=Trim('field1'), field2_new=Trim('field2')
).values('field1_new', 'field1_new').filter(someField='SomeValue').all()
return queryset