如何从StringField中剪裁/带领Whitespaces以获取Django/MongoDB中的列表视图?

发布于 2025-01-17 23:11:13 字数 945 浏览 3 评论 0原文

想要按名称对数据进行排序,但不幸的是,有许多带有领先空间的数据,这就是为什么REST_FRAMEWORK.FILTERS.FILTERS.orderingFilter无法正常工作的原因。 Mongo,DRF在我的项目中使用。

我的模型:

from mongoengine import DynamicDocument, fields

class Book(DynamicDocument):
    name = fields.StringField(required=True)
    description = fields.StringField(blank=True, null=True)

    meta = {
        'collection': 'books',
        'strict': False,
    }

我的观点:

from rest_framework.filters import OrderingFilter
from rest_framework_mongoengine import viewsets

from core.serializers import BookSerializer
from core.models import Book

class BookViewSet(viewsets.ModelViewSet):
    serializer_class = BookSerializer
    queryset = Book.objects.all()
    filter_backends = [OrderingFilter]
    ordering_fields = ['name']
    ordering = ['name']

有人有任何想法,如何解决?

Want to sort data by names, but unfortunately there are many data with leading whitespaces, that is why rest_framework.filters.OrderingFilter doesn't work properly. Mongo, DRF are used in my project.

My model:

from mongoengine import DynamicDocument, fields

class Book(DynamicDocument):
    name = fields.StringField(required=True)
    description = fields.StringField(blank=True, null=True)

    meta = {
        'collection': 'books',
        'strict': False,
    }

My view:

from rest_framework.filters import OrderingFilter
from rest_framework_mongoengine import viewsets

from core.serializers import BookSerializer
from core.models import Book

class BookViewSet(viewsets.ModelViewSet):
    serializer_class = BookSerializer
    queryset = Book.objects.all()
    filter_backends = [OrderingFilter]
    ordering_fields = ['name']
    ordering = ['name']

Someone has any idea, how to solve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

汹涌人海 2025-01-24 23:11:13

有汇总操作称为 $ trim 可以用来消毒字符串数据,如果仅要删除前导空间,则可以使用 $ ltrim ...

There is aggregation operation called $trim in mongoDB that you can use to sanitize the string data , if you want to remove only the leading spaces then you can use the $ltrim ...

友欢 2025-01-24 23:11:13

如果有人处理mongoEngine,则可以使用querySet.aggregate()

class BookViewSet(viewsets.ModelViewSet):
    serializer_class = BookSerializer
    queryset = Book.objects.all()

    def get_queryset(self):
        queryset = self.queryset
        pipeline = [
            {
                '$project': {
                    'id': {'$toString': '$_id'},
                    'name': 1,
                    'ltrim_lower_name': {'$ltrim': {'input': {'$toLower': '$name'}}},
                    'description': 1,
                }
            },
            {'$sort': {'ltrim_lower_name': 1}}
        ]

        return queryset.aggregate(pipeline)

hester python'trim_lower_name':{'$ ltrim':{'input ':{'$ tolower':'$ name'}}}},因为需要对案例不敏感的排序。

If someone deals with mongoengine, you can solve this problem with queryset.aggregate():

class BookViewSet(viewsets.ModelViewSet):
    serializer_class = BookSerializer
    queryset = Book.objects.all()

    def get_queryset(self):
        queryset = self.queryset
        pipeline = [
            {
                '$project': {
                    'id': {'$toString': '$_id'},
                    'name': 1,
                    'ltrim_lower_name': {'$ltrim': {'input': {'$toLower': '$name'}}},
                    'description': 1,
                }
            },
            {'$sort': {'ltrim_lower_name': 1}}
        ]

        return queryset.aggregate(pipeline)

Used python 'trim_lower_name': {'$ltrim': {'input': {'$toLower': '$name'}}} because need case-insensitive sorting.

假装不在乎 2025-01-24 23:11:13

还为此问题找到了一个决定,

utils.py:

collation = dict(
    locale='en',
    caseLevel=False,
    caseFirst='off',
    strength=1,
    numericOrdering=True,
    alternate='shifted',
    maxVariable='space',
    backwards=False,
)

in views.py:

class BookViewSet(viewsets.ModelViewSet):
    serializer_class = BookSerializer
    queryset = Book.objects.all()

    def get_queryset(self):
        queryset = self.queryset
        return queryset.collation(collation)

Also find one more decision for this question

utils.py:

collation = dict(
    locale='en',
    caseLevel=False,
    caseFirst='off',
    strength=1,
    numericOrdering=True,
    alternate='shifted',
    maxVariable='space',
    backwards=False,
)

In views.py:

class BookViewSet(viewsets.ModelViewSet):
    serializer_class = BookSerializer
    queryset = Book.objects.all()

    def get_queryset(self):
        queryset = self.queryset
        return queryset.collation(collation)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文