编辑表单中的Django-Taggit [英] Django-Taggit in Edit Form

查看:122
本文介绍了编辑表单中的Django-Taggit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个模型类

class ModelName(models.Model):
  (...)
  pasta = TaggableManager(verbose_name=u'Pasta')

和表单模板(正常: P)

and a form template (normal :P )

{{form.as_p}}

{{form.as_p}}

我想把一切都很干净,有用。
但结果是TaggedItem对象的列表:(:

I'd like to leave everything very clean and usefull. But result is a list of TaggedItem Object :( :

[<TaggedItem: id: 2 tagged with general >, <TaggedItem: id: 3  tagged with outer >]

而不是像

general, outer

在Django?

推荐答案

查看代码: https://github.com/alex/django-taggit/blob/master/taggit/forms.py 。您可以找到用于渲染标签的小部件,您可以使用它来正确地呈现它们。

Give a look at the code in: https://github.com/alex/django-taggit/blob/master/taggit/forms.py. You will find the widget used to render the tags. You can use it to render them correctly.

示例:

models.py

models.py

from django.db import models
from taggit.managers import TaggableManager


class Example(models.Model):
    name = models.CharField(max_length=20)    
    tags = TaggableManager()

forms.py

.models import Example
from django import forms
from taggit.forms import TagWidget


class ExampleForm(forms.ModelForm):

    class Meta:
        model = Example
        fields = ('name', 'tags',)
        widgets = {
            'tags': TagWidget(),
        }

'建议你也来检查这个答案。
django - django-taggit表单

I'd recommend you to check this answer too. django - django-taggit form

这篇关于编辑表单中的Django-Taggit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆