models.py
class Tags(models.Model):
name = models.CharField(max_length = 36, verbose_name = _(u'标签'))
slug = models.SlugField(max_length = 50, verbose_name = _(u'标签url'), default = 'tags')
num = models.PositiveIntegerField(verbose_name = _(u'标签排序'), default = 0)
def __unicode__(self):
return self.name
class Meta:
verbose_name_plural = u'标签名字'
def get_absolute_url(self):
return reverse('info:tags', kwargs = {'slug':self.slug})
forms.py
class CreateTopicForm(forms.ModelForm):
tags = forms.ModelMultipleChoiceField(queryset=Tags.objects.all(), widget=forms.CheckboxSelectMultiple(), required=False)
category = forms.ModelChoiceField(queryset=Category.objects.all(), widget=forms.RadioSelect(), initial={'headline': 'Initial headline'})
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user',None)
super(CreateTopicForm, self).__init__(*args, **kwargs)
class Meta:
model = Topic
fields = ['title', 'category', 'body', 'tags',]
def save(self, commit=True):
inst = super(CreateTopicForm, self).save(commit = False)
inst.author = self.user
if commit:
inst.save()
self.save_m2m()
return inst
views.py
import jieba
import jieba.posseg as pseg
pctaglist = ['高兴', '你好', '上海', '抑郁症', '头晕']
@login_required
def create_info(request):
if request.method == "POST":
form = CreateTopicForm(request.POST, user=request.user)
if form.is_valid():
body = form.cleaned_data['body']
t = form.save(commit=False)
t.ip_user = request.META['REMOTE_ADDR']
words = list(jieba.cut(body, cut_all = True))
t = form.save()
tags_list = []
for word in words:
if word in pctaglist:
tags_list.append(word)
t.tags.add(word)
#问题主要在这里, pctaglist 是在后台添加的 标签tags, #用jieba分词分析文章内容后得出来的词与这个后台添加的标签取交集。如果有就添加到这个文章的标签里!
#这里不知道怎么在这里添加,用t.tags.add(word) 出错:'decimal' codec can't encode characters in position 0-1: invalid decimal Unicode string
#要想实现 结巴分词,和现有的 tag 取交集后得出来的词怎么添加到文章的tags里?
return HttpResponseRedirect(reverse('info:info_detail', kwargs={'pk':t.pk}))
else:
form = CreateTopicForm()
tags_list = []
return render(request, 'info/post.html', context = {'form':form, 'tags_list':tags_list})
问题主要在这里, pctaglist 是在后台添加的 标签tags,
用jieba分词分析文章内容后得出来的词与这个后台添加的标签取交集。如果有就添加到这个文章的标签里!
这里不知道怎么在这里添加,
用t.tags.add(word) 出错:'decimal' codec can't encode characters in position 0-1: invalid decimal Unicode string 我也觉得这里这样操作有问题.但是一时不知道怎么改.知识不够....
要想实现 结巴分词,和现有的 tag 取交集后得出来的词怎么添加到文章的tags里?
或者有其它的好办法实现这一步?
谢谢~
根据错误提示。t.tags应该是个数字数组吧,你把字符串加到数字数组里了吧。