【问题标题】:How to make django-taggit admin text input field a bigger size?如何使 django-taggit 管理文本输入字段更大?
【发布时间】:2014-08-19 00:58:39
【问题描述】:

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

tags = TaggableManager()

谢谢

【问题讨论】:

    标签: django django-taggit


    【解决方案1】:

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

    但是,您需要覆盖任何 admin.widget,例如 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
            }
    

    【讨论】:

      猜你喜欢
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-05
      • 2012-07-30
      • 2015-09-05
      • 2019-10-31
      相关资源
      最近更新 更多