一个可重用的django应用程序,允许您向任何其他模型添加可翻译的标记。
django-multilingual-tags的Python项目详细描述
一个可重用的django应用程序,允许您将可翻译的标记添加到任何其他 模型。
安装
从pypi获得最新的稳定版本
pip install django-multilingual-tags
从github获取最新提交
pip install -e git+git://github.com/bitmazk/django-multilingual-tags.git#egg=multilingual_tags
将multilingual_tags添加到INSTALLED_APPS
INSTALLED_APPS=(...,'multilingual_tags',)
不要忘记迁移数据库
./manage.py migrate multilingual_tags
用法
向模型添加要标记的泛型关系。
fromdjango.contrib.contenttypesimportgenericfromdjango.dbimportmodelsclassUserProfile(models.Model):# ...# some other model stuff goes here# ...special_attributes=generic.GenericRelation('multilingual_tags.TaggedItem',)
将TaggingFormMixin添加到模型的任何模型表单中,您可以 使用设置字典标记和配置字段。allowed_tags 配置用户可以添加的标记数。如果设置为0(默认),则表示, 输入不受限制。
fromdjangoimportformsfrommultilingual_tags.forms.mixinsimportTaggingFormMixinclassUserProfileForm(TaggingFormMixin,forms.ModelForm):tag_field={# ``name`` is the name of the ``GenericRelation`` that was added to# the model'name':'special_attributes','label':_('Special Attributes'),'help_text':_('List any special attributes separated with comma.'),'required':False,'max_tags':0,}
表单MIXIN将自动添加^ {TT5}$ 到表单域。这允许您轻松地添加jquery-typeahead-tagging 到您的字段,该字段包含在此应用程序中。
只需将multilingual_tags中的静态文件添加到模板中。
{% load static %} {# Plain Bootstrap-like styles. #} <linkhref="{% static "multilingual_tags/css/typeahead.tagging.css"%}"rel="stylesheet"media="screen"> {# You will also need jquery of course. #} <scriptsrc="{% static "js/libs/jquery-1.9.1.js"%}"></script> {# And then there's typeahead and the tagging plugin. #} <scriptsrc="{% static "multilingual_tags/js/typeahead.bundle.min.js"%}"></script><scriptsrc="{% static "multilingual_tags/js/typeahead.tagging.js"%}"></script>
然后您可以像这样初始化标记字段:
// The source of the tags for autocompletion vartagsource=['Foo','Bar','Anoter Tag','Even more tags','Such autocomplete','Many tags','Wow'];// Turn the input into the tagging input $('[data-class="multilingual-tags-field"]').tagging(tagsource);
等一下!那应该就是全部了。
存储用户
为了以后更容易访问,您可以存储标记项所属的用户 到TaggedItem本身。表单MIXIN将检查实例是否 与它一起使用的ModelForm有一个get_user方法。
classMyObject(models.Model):# here goes my model implementationdefget_user(self):"""Here I can return the user I want on the TaggedItem"""returnself.user
管理
要向模型添加标记,必须将TaggedItemInline添加到 那个模特是管理员。在您自己的应用程序中,您只需执行以下操作:
fromdjango.contribimportadminfrommultilingual_tags.adminimportTaggedItemInlinefrommy_appimportmodelsclassMyModelAdmin(admin.ModelAdmin):inlines=[TaggedItemInline]admin.site.register(models.MyModel,MyModelAdmin)
这将呈现用于添加标记项的内联管理表单。
如果要向第三方应用程序添加标记,可能需要导入其 管理员而不是django的ModelAdmin,然后注销并重新注册 模型。一种方法是这样:
fromdjango.contribimportadminfrommultilingual_tags.adminimportTaggedItemInlinefromother_app.adminimportSomeModelAdminfromother_app.modelsimportSomeModelclassSomeModelCustomAdmin(SomeModelAdmin):# be careful, if the other admin also defines admins, you need to add# them as wellinlines=SomeModelAdmin.inlines+[TaggedItemInline]admin.site.unregister(SomeModel)admin.site.register(SomeModel,SomeModelCustomAdmin)
要获取对象的所有标记,只需使用标记管理器
# Get all tags for a certain model instance>>Tag.objects.get_for_obj(mymodel_instance)[<Tag:mytag>,<Tag:myothertag>]# .. or get all tags for an entire queryset>>Tag.objects.get_for_queryset(MyModel.objects.all())[<Tag:mytag>,<Tag:myothertag>]
贡献
如果您想参与此项目,请执行以下步骤
# Fork this repository # Clone your fork mkvirtualenv -p python2.7 django-multilingual-tags make develop git co -b feature_branch master # Implement your feature and tests git add . && git commit git push -u origin feature_branch # Send us a pull request for your feature branch