如何让django-taggit管理文本输入字段更大?

时间:2014-06-25 08:56:31

标签: django django-taggit

根据标题,我如何告诉管理界面(admin.py)为我的模型的标签字段使用自定义大小,该字段使用django-taggit:

tags = TaggableManager()

由于

1 个答案:

答案 0 :(得分:0)

Django Admin - Overriding the widget of a custom form field类似的问题。

但是,您需要覆盖任何admin.widgets,例如AdminTextareaWidget,以便正确显示标记。否则,表单字段值将显示为

[<TaggedItem: Product object tagged with bar>, 
<TaggedItem: Product object tagged with foo>, 
<TaggedItem: Product object tagged with baz>]

示例代码来说明这一点。

#forms.py

from django import forms
from django.contrib.admin.widgets import AdminTextareaWidget
from django.utils import six

from models import Product

from taggit.utils import edit_string_for_tags


class TaggitAdminTextareaWidget(AdminTextareaWidget):
    # taken from taggit.forms.TagWidget
    def render(self, name, value, attrs=None):
        if value is not None and not isinstance(value, six.string_types):
            value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
        return super(TaggitAdminTextareaWidget, self).render(name, value, attrs)


class ProductAdminForm(forms.ModelForm):
    class Meta:
        model = Product
        widgets = {
            'tags': TaggitAdminTextareaWidget
        }