创建模型时如何覆盖/更新 POST 信息
我有一个处理 POST 请求并尝试创建新对象的视图。但是,我知道某些 POST 数据无效...但我想修复它并继续创建对象。
我能想到能够在 ModelForm 中“修复”数据的唯一方法是创建一个“is_valid()”表单。为此,我可以使用 POST 数据创建表单,也可以使用现有实例创建表单。不幸的是,如果我使用 POST 数据,因为其中一些数据无效,表单将无法验证,因此我无法获取表单中的数据来修复它。如果我使用已经存在的实例创建它,这可以工作,但是当显示表单时,任何剩余的错误都会因任何原因被忽略(因此不会显示在网页上)。我尝试了从 POST 数据创建模型表单并为其提供一个实例的组合,但这似乎没有帮助。此外,我尝试修改 POST 数据(的副本)、修复它,然后从“固定”POST 数据创建 ModelForm。这种方法是有效的,除了我的表单中有一些 ImageFields 之外,它们似乎只是被忽略了。
也许有更好的方法来做到这一点?我试图解决的问题是我想要一个包含 ImageFields 的模型。我第一次提交表单时,用户需要“上传”每个字段的图像。但是,如果他没有更新其中一个字段的图像,我希望新表单在尚未上传图像的字段上显示一个图像上传按钮,并且只是一个带有图像图像名称的文本字段已上传。
好的,我想我可以将上述所有问题简化为:
def testing(request): test_form = UserProfileForm() valid = test_form.is_valid() return render('testing.tmpl', locals(), request)
当呈现上述代码时,“valid”显示为 False(正如人们所期望的那样),但“test_form”呈现时没有任何错误。我已经通读了(如果可能不理解?)有关模型和模型表单的文档,我发现大多数时候模型表单(在我的例子中:UserProfileForm)是使用指定的“实例”创建的。但是,1)我还没有实例,2)我仍然希望非实例化表单显示错误。我确信我缺少一些东西。请照亮。 :)
还有一件事,也许上面的答案无论如何都会回答,但据我所知, is_valid() 调用应该调用我为 UserProfileForm 定义的“clean()”函数。然而,(不是Python大师)我将“raise ValidationError()”放在clean()的顶部,当我运行代码时,没有显示错误。想法?
I have a view that handles a POST request and attempts to create a new object. However, I know that some of the POST'd data is invalid... But I want to fix it and go ahead and create the object.
The only way I can figure out to be able to 'fix' data in a ModelForm is to create a 'is_valid()' form. To do this, I can either create the form with the POST data, or I can create it with an already existing instance. Unfortunately, if I use the POST data, because some of it is invalid, the form won't validate and I am thus unable to get to the data in the form to fix it. If I create it with an already existing instance, this works, but when the form is displayed, any remaining errors are for whatever reason ignored (and thus don't show up on the web page). I've tried a combination of creating the the Model form from the POST data and giving it an instance, but this doesn't seem to help. Additionally, I've tried modifying (a copy of) the POST data, fixing it, and then creating the ModelForm from the 'fixed' POST data. This sort of works, with the exception that I have some ImageFields in my form, and they seem to just be ignored.
Perhaps there is a better way to do this? The problem I'm trying to solve is that I want to have a model that contains ImageFields. The first time I put up the form, the user needs to 'upload' images for each of the fields. However, if he doesn't update an image for one of the fields, I want the new form to come up with a Image upload button on the fields where images have not been uploaded, and just a text field with the image name for images that have been uploaded.
Ok, I think I can simplify all of the above question into this:
def testing(request): test_form = UserProfileForm() valid = test_form.is_valid() return render('testing.tmpl', locals(), request)
When the above code is rendered, the 'valid' shows as False (as one might expect), but the 'test_form' renders without any errors. I've read through (if perhaps not understood?) the documentation on Models and ModelForms, and I see that most of the time a ModelForm (in my case: UserProfileForm) is created with a specified 'instance'. However, 1) I don't have an instance yet, 2) I would still expect the non-instance'd Form to display errors. I'm sure there is something I am missing. Please illuminate. :)
One more thing, which perhaps the answer to the above will answer anyway, but as far as I can tell, the is_valid() call is supposed to call the 'clean()' function I defined for the UserProfileForm. However, (not being a python guru) I placed 'raise ValidationError()' at the top of clean(), and when I run the code, no error is shown. Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该看看如何清理Django 中的表单字段。您可以操纵从表单返回的数据或进行任何类型的验证!
You should have a look at how to clean form fields in Django. You could either manipulate the data returned from the form there or make any kind of validation!
如果您的 ImageFields 是可选的,那么您仍然可以验证它们(它们在其他方面是正确的)。
然后,需要调整模板以显示上传的文件名或文件上传字段,具体取决于他们是否已经上传。实际上,在第一种情况下给它们两个字段可能会更好。这就是自动管理员所做的(上传字段标记为“更改”)。
If your ImageFields are optional then you can still validate them (that they are otherwise correct).
Then it's a matter of adjusting your template to show either the uploaded file name or an file upload field depending on whether they've already uploaded one or not. Actually, it would probably be better to give them both fields in the first case. That's what the automatic admin does (the upload field is labeled "Change").
在弄清楚如何使用 Python 调试器 (pdb) 以及在 Emacs 中的事实之后当我看到“Just Works”时,我发现我的(空)表单没有被绑定。谷歌搜索绑定表单将我指向此页面:
表单 API
RTFM'ing 我发现我可以将一个空字典传递给我的表单,然后一切都开始按照我的预期运行。因此,总而言之,两者之间存在很大差异:
和
第二个版本导致表单的呈现显示所有错误(并调用“clean()”)。
After figuring out how to use the Python debugger (pdb) and the fact that within Emacs it kind of 'just works', I was able to find that my (empty) form was not bound. Googling bound forms pointed me to this page:
The Forms API
RTFM'ing I find that I can pass an empty dictionary to my form and then everything starts to behave as I would expect. So, to summarize, there is a big difference between:
and
The second version causes the rendering of the form to show all the errors (and to call 'clean()').