django/taggit - unhashable type: 'list'

此生再无相见时 提交于 2019-12-06 15:02:37

问题


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

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

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!


回答1:


You need to either add tags individually:

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

this is equivalent to:

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

or pass them as positional arguments to tags.add:

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

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



来源:https://stackoverflow.com/questions/5547142/django-taggit-unhashable-type-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!