简体   繁体   English

更改 django-taggit 中标签的输入方式

[英]Changing the way tags in django-taggit are input

I'm trying out django-taggit and really enjoying it.我正在尝试 django-taggit 并且非常喜欢它。 One thing that I can't figure out tho, is how to change the default comma-separated tag input into the django default many-to-many field with the filter_horizontal option (or even checkboxes).我无法弄清楚的一件事是如何使用 filter_horizo​​ntal 选项(甚至复选框)将默认的逗号分隔标签输入更改为 django 默认的多对多字段。 It would make more sense to what I want to do as I only want admins to be able to create tags, with content producers just choosing from the available ones这对我想做的事情更有意义,因为我只希望管理员能够创建标签,内容制作者只需从可用标签中进行选择
Found a similar question here在这里找到了一个类似的问题
Using Check boxes in Taggit 在 Taggit 中使用复选框
But I can't make sense of what has to be plugged where to make it happen但我无法理解必须将什么插入到哪里才能实现

Don't have the time to try to understand how taggit really works in order to to what I need, so I came up with a quick workaround - created a method inside the model that retrieves all available tags and displays them in the help text for the tags field.没有时间尝试了解 taggit 的实际工作原理以满足我的需要,因此我想出了一个快速的解决方法 - 在模型中创建一个方法来检索所有可用的标签并将它们显示在帮助文本中标签字段。

tags = TaggableManager(blank=True, help_text = tag_helptext())    
def tag_helptext():
    help_text = "Options: "
    for t in Tag.objects.all():
        help_text += t.name + " ||| "
    return help_text

Then in the admin I removed priviledges to create new tags to everyone but the superuser.然后在管理员中,我删除了为除超级用户之外的所有人创建新标签的权限。
Feels kinda hackish, but provides what I needed (to make it easy for users to use existing tags and avoid them creating new ones)感觉有点 hackish,但提供了我需要的东西(让用户更容易使用现有标签并避免他们创建新标签)

I came across the same problem, so I'm posting anyways to share my solution.我遇到了同样的问题,所以无论如何我都会发布以分享我的解决方案。 Extract from the point 7.2 this document摘自本文档的第 7.2 点

To provide your own parser, write a function that takes a tag string and returns a list of tag names.要提供您自己的解析器,请编写一个接受标签字符串并返回标签名称列表的函数。 For example, a simple function to split on comma and convert to lowercase:例如,一个用逗号分割并转换为小写的简单函数:

def comma_splitter(tag_string):
return [t.strip().lower() for t in tag_string.split(',') if t.strip()]

You need to tell taggit to use this function instead of the default by adding a new setting, TAGGIT_TAGS_FROM_STRING and providing it with the dotted path to your function.您需要通过添加新设置TAGGIT_TAGS_FROM_STRING并为其提供函数的虚线路径来告诉 taggit 使用此函数而不是默认函数。 Likewise, you can provide a function to convert a list of tags to a string representation and use the setting TAGGIT_STRING_FROM_TAGS to override the default value (which is taggit.utils._edit_string_for_tags ):同样,您可以提供将标签列表转换为字符串表示形式的函数,并使用设置TAGGIT_STRING_FROM_TAGS覆盖默认值(即taggit.utils._edit_string_for_tags ):

def comma_joiner(tags):
return ', '.join(t.name for t in tags)

If the functions above were defined in a module, appname.utils, then your project settings.py file should contain the following:如果上述函数是在模块 appname.utils 中定义的,那么您的项目 settings.py 文件应包含以下内容:

TAGGIT_TAGS_FROM_STRING = 'appname.utils.comma_splitter'
TAGGIT_STRING_FROM_TAGS = 'appname.utils.comma_joiner'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM