我把save()写在view中,怎么获得WeiboImage中的weibo并将其与Weibo关联起来
来源:4-2 文件上传之ORM上传
老中医学编程
2019-07-30 01:59:07
views:
def file_upload_weibo(request):
'''发送微博带图片'''
#因为微博没登录,默认用户来进行微博的发布
if request.method == "POST":
form = WeiboImageForm(request.POST,request.FILES)
if form.is_valid():
user = WeiboUser.users.get(pk=21) # 假设微博已登录的用户为id=21
form.save(commit=False) #只创建实例,但是不保存
content = request.POST.get('content') #从前端post回来的内容
weibo = Weibo.objects.create(user=user,content=content)
# ?怎么获得WeiboImage中的weibo并将Weibo关联起来
form.save()
print('保存成功')
else:
form = WeiboImageForm()
return render(request,'file_upload_weibo.html',{
'form':form
})1回答
时间,
2019-07-30
同学,你好。
1、同学可以将from.save()返回的对象存在变量obj中,将创建的weibo重新赋值给obj中的weibo,再调用save进行保存。
2、同学只提供了部分代码,无法确定同学的WeiboImageForm()中的save方法是如何写的,上述方法可做参考,建议同学根据视频的讲解写代码,将保存的逻辑写在save方法中,在使用时直接调用即可。
如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~
相似问题