问题
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