Django:在多表单视图中引用pk
我有两种形式的视图。我试图以相同的形式引用上传图像的 pk。我该怎么做?
form1 = ImageForm(request.FILES, request.POST)
form2= SongForm(request.POST or None)
if form2.is_valid() and form1.is_valid():
image = form1.save(commit=False)
image.save()
save_file(request.FILES['image'])
song = form2.save(commit=False)
song.image = the uploaded image's pk???
song.save()
谢谢,
I have a view with two forms. I am trying to reference the uploaded image's pk in the same form. How do i do that?
form1 = ImageForm(request.FILES, request.POST)
form2= SongForm(request.POST or None)
if form2.is_valid() and form1.is_valid():
image = form1.save(commit=False)
image.save()
save_file(request.FILES['image'])
song = form2.save(commit=False)
song.image = the uploaded image's pk???
song.save()
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦调用
image.save()
,图像就应该获得一个pk,因此,您应该能够使用image.pk
引用它。Once you call
image.save()
image should get a pk, and therefore, you should be able to reference it withimage.pk
.