Django:在多表单视图中引用pk

时间:2011-05-13 16:45:11

标签: django image file-upload django-forms django-views

我有两种形式的观点。我试图以相同的形式引用上传的图像的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()

谢谢,

1 个答案:

答案 0 :(得分:1)

致电image.save()后,图片应获得pk,因此,您应该可以使用image.pk来引用它。