django/taggit - 不可散列的类型:'list' [英] django/taggit - unhashable type: 'list'

查看:27
本文介绍了django/taggit - 不可散列的类型:'list'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 django-taggit(请参阅此处).这就是我所拥有的:

I'm using django-taggit (see here). This is what I have:

forms.py

from taggit.forms import *

    class MyForm(forms.Form):
        title = forms.CharField()
        my_tags = TagField(max_length=800, widget=forms.TextInput(attrs={'class':'myTags'}))

views.py

       if 'submit_button' in request.POST:
            form = MyForm(request.POST)
            if form.is_valid():
               cd = form.cleaned_data 
               f_title = cd['title']
               f_my_tags = cd['my_tags']

               p = MyData.objects.create(title=f_title)   
               p.tags.add(f_my_tags)
               p.save()

mytemplate.html

mytemplate.html

{{ form.my_tags.errors }}
{{ form.my_tags }}

不确定为什么我在 views.py 中使用 p.tags.add(f_my_tags) 时得到 unhashable type: 'list'.有任何想法吗?谢谢!

Not sure why I get unhashable type: 'list' when I use p.tags.add(f_my_tags) in my views.py. Any ideas? Thank you!

推荐答案

您需要单独添加标签:

map(p.tags.add, cd['my_tags'])

这相当于:

for tag in cd['my_tags']:
    p.tags.add(tag)

或将它们作为位置参数传递给 tags.add:

or pass them as positional arguments to tags.add:

p.tags.add(*cd['my_tags'])

这相当于:p.tags.add(cd['my_tags'][0], cd['my_tags][1]...)

this being equivalent to: p.tags.add(cd['my_tags'][0], cd['my_tags][1]... )

这篇关于django/taggit - 不可散列的类型:'list'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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