这是模型
class Question(models.Model):
question = models.CharField(max_length = 200)
questionbody = models.TextField()
author = models.ForeignKey(User)
tags = models.ManyToManyField(Tag)
timestamp = models.DateTimeField('question post date')
这是 view.py
stags = request.POST['tag']
ltags = stags.split(',')
q = Question(
question = request.POST['question'],
questionbody = request.POST['questionbody'],
author = request.user,
timestamp = datetime.datetime.now()
)
q.save();
for i in ltags:
# print i ouyput:jquery
# print type(i) ouyput:unicode
# s = Tag.objects.get(name=i)
s = Tag.objects.get(name='jquery')
q.tags.add(s)
如您所见,“标签”输入采用“字符串,字符串”格式字符串,因此我将其更改为列表(ltags),
但是在for循环中,当我尝试添加标签时
s = Tag.objects.get(name=i)
提问,有一个错误说
" Tag matching query does not exist ".
但是当我使用 s = Tag.objects.get(name='jquery') 时,它可以工作。
请帮忙!