Django的ClearableFileInput小部件在提交时不会传递初始(“Currently”)值 [英] Django's ClearableFileInput widget doesn't pass initial ("Currently") value on submit

查看:500
本文介绍了Django的ClearableFileInput小部件在提交时不会传递初始(“Currently”)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基本目标是创建一个表单(使用 ModelForm ),预先填充现有数据库对象的数据,允许用户修改这些数据,然后保存提交的表单作为新的对象。 (排序的复制,然后修改粘贴设置。)除了文件字段和ClearableFileInput小部件之外,这可以正常工作。

My basic goal is to create a form (using ModelForm) prefilled with data from an existing database object, allow a user to modify those data, then save the submitted form as a new object. (Sort of a "Copy" then "Paste With Modifications" setup.) This works fine except with File Fields and the ClearableFileInput widget.

这个这个表明文件字段不能也不应该用初始值填充。

The answers to questions like this, this, and this make it clear that file fields can't and shouldn't be populated with an initial value.

然而,Django文档在 ClearableFileInput 小部件说:

The Django docs, however, on the ClearableFileInput widget say:


文件上传输入:< input type ='file'...> / code>,并附加一个复选框输入,以清除字段的值,如果该字段不是必需,并且具有 初始 数据。

File upload input: <input type='file' ...>, with an additional checkbox input to clear the field's value, if the field is not required and has initial data.

(有点令人困惑,但是我假设Django引用具有初始值的模型字段,而< input type ='file'...> 字段是仍然空白?)

(A bit confusing already, but I assume Django's referring to the model field having an initial value, whereas the <input type='file' ...> field is still blank?)

所以如果我使用这种方法:

So if I use either this method:

>>> obj = Foo.objects.get(pk=1)
>>> obj.fooFile.name
u'files/foo1.txt'
>>> f = FooForm(instance=obj)

或此方法:

f = FooForm(initial={'fooFile':Foo.objects.get(pk=1).fooFile})

要实例化我的表单,ClearableFileInput小部件显示:

to instantiate my form, the ClearableFileInput widget displays:

目前有清除复选框,更改...输入http://phagesdb.org/static/formDisplay.png

当用户提交表单而不更改此字段时,当前行的值似乎完全失去了Django。它似乎不在 request.POST form.cleaned_data 中。当我在我绑定的ModelForm上调用 .is_valid()然后 .save()时,文件字段最后为空在新对象中。

When the user submits the form without changing this field, however, the value from the "Currently" line seems to be totally lost to Django. It doesn't appear to be in either request.POST or form.cleaned_data. When I call .is_valid() and then .save() on my bound ModelForm, the file field ends up blank in the new object.

所以:


  1. ClearableFileInput小部件传递当前值吗?如果不是,ClearableFileInput小部件不是误导和/或非常有限的使用?我假设返回的值将是Currently ...中的值,除非我使用Change ...输入。

  1. Does the ClearableFileInput widget pass the "Currently" value or not? If not, isn't the ClearableFileInput widget misleading and/or of very limited use? I'd assume that the value returned would be the value in "Currently..." unless I use the "Change..." input.

当我调用 .save()时,我可以捕获该值,以便它出现在我的新对象中吗? (请记住,我不能使用 f = FooForm(request.POST,request.FILES,instance = obj),因为我想要一个新对象,而不是修改的对象。

How can I capture that value so it appears in my new object when I call .save()? (Remember that I can't use f = FooForm(request.POST, request.FILES, instance=obj) because I want a new object, not a modified object.)


推荐答案

将隐藏的字段放在窗体中,并用路径预先填充以你最初传入的形象。然后写一个保存方法,如下所示。

Place a hidden field in your form and prepopulate it with the path to the image you passed in as initial. Then write a save method something like the following.

这将向用户显示,就像文件域被预填充一样,将文件保存到新实例,并允许它们使用清除复选框,或上传自己的图像。

This will appear to the user as if the filefield is prepopulated, save the file to the new instance, and allow them to use the clear checkbox, or upload their own image.

from django.core.files.base import ContentFile
def save(self, commit=True):
    insance = super(FormClass, self).save(False)
    if not instance.file_field and self.cleaned_data['file_field_path'] and not self.data.get('file_field-clear'):
        path = os.path.join(settings.MEDIA_ROOT, self.cleaned_data['file_field_path'])
        with open(path) as f:
            file_object = ContentFile(f.read())
            instance.file_field.save(self.cleaned_data['file_field_path'], file_object, False)

    if commit:
        insance.save()
    return insance

这篇关于Django的ClearableFileInput小部件在提交时不会传递初始(“Currently”)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆