使用 django-taggit 制作不区分大小写的标签

发布于 2024-10-21 14:02:12 字数 172 浏览 2 评论 0原文

我向我的模型添加了 Tags = TaggableManager(blank=True) ,但我希望我的标签不区分大小写。 我看到了一些解决此问题的片段,想问是否有一种简单的方法可以做到这一点? 如果我必须重写 TaggableManager 中的某些方法,请告知我该怎么做?

提前致谢, 阿尔沙夫斯基·亚历山大.

I added tags = TaggableManager(blank=True) to my models, but I want my tags to be case-insensitive.
I saw some snippets of work arounds for this and wanted to ask if there is an easy way to do it?
If I have to override some methods from the TaggableManager, please advise how can I do that?

Thanks in advance,
Arshavski Alexander.

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

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

发布评论

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

评论(4

会傲 2024-10-28 14:02:12

我相信你现在已经找到了解决方案:)尽管我会给出答案,因为有人在搜索时可能会遇到这个问题 -

使用 https://github.com/shacker/django-taggit 版本的 django-taggit。

它允许配置 TAGGIT_FORCE_LOWERCASE = True

它还允许您过滤停用词。

I am sure you have figured out solution by now :) Though I will put the answer as someone might hit this while searching -

Use https://github.com/shacker/django-taggit version of django-taggit.

It allows to configure TAGGIT_FORCE_LOWERCASE = True

It also allows you to filter stop words.

心意如水 2024-10-28 14:02:12

最新版本的 django-taggit 支持 TAGGIT_CASE_INSENSITIVE 设置,其行为应如您所描述的方式。

Recent versions of django-taggit support a TAGGIT_CASE_INSENSITIVE setting, that should behave the way you're describing.

吝吻 2024-10-28 14:02:12

根据211行的_TaggableManager,

case_insensitive = getattr(settings, "TAGGIT_CASE_INSENSITIVE", False)

我们需要在settings.py中添加这个参数

According to _TaggableManager on 211 line

case_insensitive = getattr(settings, "TAGGIT_CASE_INSENSITIVE", False)

we need to add this parameter in settings.py

偏闹i 2024-10-28 14:02:12

我已经使用此代码段来解决类似的问题。代码的副本(为后代重印):

from django.db.models import Manager
from django.db.models.query import QuerySet

class CaseInsensitiveQuerySet(QuerySet):
    def _filter_or_exclude(self, mapper, *args, **kwargs):
        # 'name' is a field in your Model whose lookups you want case-insensitive by default
        if 'name' in kwargs:
            kwargs['name__iexact'] = kwargs['name']
            del kwargs['name']
        return super(CaseInsensitiveQuerySet, self)._filter_or_exclude(mapper, *args, **kwargs)

# custom manager that overrides the initial query set
class TagManager(Manager):
    def get_query_set(self):
        return CaseInsensitiveQuerySet(self.model)

# and the model itself
class Tag(models.Model):
    name = models.CharField(maxlength=50, unique=True, db_index=True)

    objects = TagManager()

    def __str__(self):
        return self.name
# now...
>>> tag = Tag(name='test')
>>> tag.save()
>>> Tag.objects.get(name='TEST')
<Tag: test>

基本上,您覆盖初始查询集,以便所有查询集都考虑不区分大小写。

I have used this snippet for a similar issue. A copy of the code (reprinted for posterity):

from django.db.models import Manager
from django.db.models.query import QuerySet

class CaseInsensitiveQuerySet(QuerySet):
    def _filter_or_exclude(self, mapper, *args, **kwargs):
        # 'name' is a field in your Model whose lookups you want case-insensitive by default
        if 'name' in kwargs:
            kwargs['name__iexact'] = kwargs['name']
            del kwargs['name']
        return super(CaseInsensitiveQuerySet, self)._filter_or_exclude(mapper, *args, **kwargs)

# custom manager that overrides the initial query set
class TagManager(Manager):
    def get_query_set(self):
        return CaseInsensitiveQuerySet(self.model)

# and the model itself
class Tag(models.Model):
    name = models.CharField(maxlength=50, unique=True, db_index=True)

    objects = TagManager()

    def __str__(self):
        return self.name
# now...
>>> tag = Tag(name='test')
>>> tag.save()
>>> Tag.objects.get(name='TEST')
<Tag: test>

Basically, you override the initial queryset so that all querysets take case insensitivity into account.

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