django - django-taggit 形式

发布于 2024-10-23 23:00:01 字数 526 浏览 2 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如何视而不见 2024-10-30 23:00:01

我对 django taggit 应用程序不太熟悉,但看起来如果您想使用应用程序使用的相同字段和小部件设置,您可以从 taggit.forms 导入它们(https://github.com/alex/django-taggit/blob/master/taggit/forms.py):

your models.py:

from django.db import models

from taggit.managers import TaggableManager

class Food(models.Model):
    name = models.CharField(max_length=20)

    tags = TaggableManager()

your forms.py

from taggit.forms import *

class MyForm(forms.Form):
    name = forms.CharField()
    m_tags = TagField()

TagField 将使用 taggit 应用程序中 utils.py 中的 parse_tags 方法返回处理后的输入。返回的结果看起来是一个清理过的列表(set(words))

你的views.py

if form.is_valid():
    name = form.cleaned_data['name']
    m_tags = form.cleaned_data['m_tags']
    object = Food(name=name)
    object.save()
    for m_tag in m_tags:
        object.tags.add(m_tag)
    return HttpResponseRedirect('/thanks/')

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:

from django.db import models

from taggit.managers import TaggableManager

class Food(models.Model):
    name = models.CharField(max_length=20)

    tags = TaggableManager()

your forms.py

from taggit.forms import *

class MyForm(forms.Form):
    name = forms.CharField()
    m_tags = TagField()

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

if form.is_valid():
    name = form.cleaned_data['name']
    m_tags = form.cleaned_data['m_tags']
    object = Food(name=name)
    object.save()
    for m_tag in m_tags:
        object.tags.add(m_tag)
    return HttpResponseRedirect('/thanks/')
用心笑 2024-10-30 23:00:01

我无法对使用的/“绿色勾选”答案发表评论。但我会将块更改

for m_tag in m_tags:
    object.tags.add(m_tag)

object.tags.add(*m_tags)

I can't comment on the used/"green ticked" answer. But I would change the Block

for m_tag in m_tags:
    object.tags.add(m_tag)

to

object.tags.add(*m_tags)
多孤肩上扛 2024-10-30 23:00:01

请参阅此处的说明: https://github.com/alex/django -taggit/blob/master/docs/forms.txt

如果在保存表单时,您使用需要调用的 commit=False 选项
保存对象后,在表单上使用save_m2m(),就像保存对象一样
其上有正常的多对多字段的表单::

if request.method == "POST":
    form = MyFormClass(request.POST)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.user = request.user
        obj.save()
        # Without this next line the tags won't be saved.
        form.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 call
save_m2m() on the form after you save the object, just as you would for a
form with normal many to many fields on it::

if request.method == "POST":
    form = MyFormClass(request.POST)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.user = request.user
        obj.save()
        # Without this next line the tags won't be saved.
        form.save_m2m()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文