【发布时间】:2016-04-14 23:10:42
【问题描述】:
我正在从数据库表文章中提取一个主键,并尝试将其作为外键保存到数据库表注释中。我收到错误: int() 参数必须是字符串或数字,而不是 'QuerySet'
我尝试将 article_id 转换为 int(article_id),但它返回相同的错误。我尝试将值拉入列表,但随后返回列表错误。我打印了查询 article_id 的值,并在终端返回[{'id': 1L}],这是数据库中的第一篇文章。
这种插入外键的方法在更新记录时有效,但现在在创建记录时无效。 如何插入外键?
我正在使用 Python 2.7 和 django 1.9
if request.POST.get("ComentForm") is not None:
if InsertComent.is_valid():
article_id = Article.objects.filter(title = Slug).filter(pub_date = aTimestamp).values("id")
article_id = article_id
user_id = request.user.id
p=Comment(comment=InsertComent.cleaned_data['comment'], article_id=article_id, user_id=user_id)
p.save()
messages.add_message(request, messages.SUCCESS,
"Thanks for commenting!", extra_tags="ComentForm"
)
return HttpResponseRedirect(reverse('Article', kwargs={'aTimestamp':aTimestamp,'aSlug':aSlug}))
【问题讨论】:
标签: python mysql django foreign-keys