开发者

django/taggit - unhashable type: 'list'

开发者 https://www.devze.com 2023-02-21 21:29 出处:网络
I\'m using django-taggit (see here). This is what I have: forms.py from taggit.forms import * class MyForm(forms.Form):

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:开发者_Go百科 'list' when I use p.tags.add(f_my_tags) in my views.py. Any ideas? Thank you!


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]... )

0

精彩评论

暂无评论...
验证码 换一张
取 消