django - django-taggit 形式
我想使用django-taggit
(点击此处)。该文档(单击此处)讨论了使用 ModelForm
生成表单,但我已经有了我想使用的表单。
假设我有这样的内容:
forms.py
class MyForm(forms.Form):
......
tags = forms.CharField(max_length=200, widget=forms.Textarea)
如何保存来自 tags
字段的标签?我的 views.py
里有什么?一个真实的例子将非常感激。
I would like to use django-taggit
(click here ). The documentation ( click here) talks about using ModelForm
to generate the form but I have already my form that I would like to use.
Let's say if I have something like this:
forms.py
class MyForm(forms.Form):
......
tags = forms.CharField(max_length=200, widget=forms.Textarea)
how do I save the the tags coming from the tags
field? What goes in my views.py
? A real example would be truly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对 django taggit 应用程序不太熟悉,但看起来如果您想使用应用程序使用的相同字段和小部件设置,您可以从 taggit.forms 导入它们(
https://github.com/alex/django-taggit/blob/master/taggit/forms.py
):your models.py:
your forms.py
TagField 将使用 taggit 应用程序中 utils.py 中的 parse_tags 方法返回处理后的输入。返回的结果看起来是一个清理过的列表(set(words))
你的views.py
I'm not too familiar with the django taggit app, but it looks like if you want to use the same field and widget setup the app uses, you can import them from the taggit.forms (
https://github.com/alex/django-taggit/blob/master/taggit/forms.py
):your models.py:
your forms.py
The TagField will return the processed input using the parse_tags method from utils.py in the taggit app. The return looks to be a cleaned up list(set(words))
your views.py
我无法对使用的/“绿色勾选”答案发表评论。但我会将块更改
为
I can't comment on the used/"green ticked" answer. But I would change the Block
to
请参阅此处的说明: https://github.com/alex/django -taggit/blob/master/docs/forms.txt
如果在保存表单时,您使用需要调用的
commit=False
选项保存对象后,在表单上使用
save_m2m()
,就像保存对象一样其上有正常的多对多字段的表单::
See instructions here: https://github.com/alex/django-taggit/blob/master/docs/forms.txt
If, when saving a form, you use the
commit=False
option you'll need to callsave_m2m()
on the form after you save the object, just as you would for aform with normal many to many fields on it::