[翻译]  Django Save Incomplete Progress on Form

[CHINESE]  Django在表格上保存不完整的进展


I have a django webapp with multiple users logging in and fill in a form.

我有一个django webapp,有多个用户登录并填写表格。

Some users may start filling in a form and lack some required data (e.g., a grant #) needed to validate the form (and before we can start working on it). I want them to be able to fill out the form and have an option to save the partial info (so another day they can log back in and complete it) or submit the full info undergoing validation.

一些用户可能开始填写表单并且缺少验证表单所需的一些所需数据(例如,授权#)(并且在我们开始处理之前)。我希望他们能够填写表格并选择保存部分信息(这样他们可以重新登录并完成它的另一天)或提交正在进行验证的完整信息。

Currently I'm using ModelForm for all the forms I use, and the Model has constraints to ensure valid data (e.g., the grant # has to be unique). However, I want them to be able to save this intermediary data without undergoing any validation.

目前我正在为我使用的所有表单使用ModelForm,并且模型具有确保有效数据的约束(例如,授权#必须是唯一的)。但是,我希望他们能够保存这些中间数据,而无需进行任何验证。

The solution I've thought of seems rather inelegant and un-django-ey: create a "Save Partial Form" button that saves the POST dictionary converts it to a shelf file and create a "SavedPartialForm" model connecting the user to partial forms saved in the shelf. Does this seem sensible? Is there a better way to save the POST dict directly into the db? Or is an add-on module that does this partial-save of a form (which seems to be a fairly common activity with webforms)?

我想到的解决方案看起来相当不优雅和非常规:创建一个“保存部分表单”按钮,保存POST字典将其转换为工具架文件并创建一个“SavedPartialForm”模型,将用户连接到保存的部分表单在架子上。这看起来合情合理吗?有没有更好的方法将POST dict直接保存到db?或者是一个附加模块,它可以对表单进行部分保存(这似乎是与webforms相当常见的活动)?

My biggest concern with my method is I want to eventually be able to do this form-autosave automatically (say every 10 minutes) in some ajax/jquery method without actually pressing a button and sending the POST request (e.g., so the user isn't redirected off the page when autosave is triggered). I'm not that familiar with jquery and am wondering if it would be possible to do this.

我对我的方法最关心的是我希望最终能够在一些ajax / jquery方法中自动执行此表单 - 自动保存(比如说每10分钟)而不实际按下按钮并发送POST请求(例如,所以用户不是'在触发自动保存时重定向到页面外)。我对jquery并不熟悉,我想知道是否可以这样做。

4 个解决方案

#1


8  

There is a good solution in Pro Django by Marty Alchin. In a nutshell, you create another model that contains a hash of the form, the form field, and the value saved. When resuming, you just load according the form according to it's hash.

Marty Alchin在Pro Django中有一个很好的解决方案。简而言之,您创建另一个模型,其中包含表单的哈希,表单域和保存的值。恢复时,只需根据它的哈希表格加载。

#2


3  

before Saving:

保存前:

for field in form.fields:
    form.fields[field].required = False

then:

然后:

form.save()

#3


1  

The issue is that you have multiple Forms.

问题是你有多个表格。

Partial. Incomplete. Complete. Ready for this. Ready for that.

部分。不完整的。完成。准备好了。准备好了。

Indeed, you have a Form-per-stage of a workflow.

实际上,您有一个工作流程的每阶段表单。

Nothing wrong with this at all.

这完全没有错。

  1. Figure out where in the workflow you are.

    找出你在工作流程中的位置。

  2. Populate and present the form for the next stage.

    填充并呈现下一阶段的表单。

Forms can inherit from each other to save repeating validation methods.

表单可以相互继承以保存重复的验证方法。

#4


0  

Place the following into your form __init__

将以下内容放入表单__init__

for field in form.fields:
    form.fields[field].required = False

For example:

例如:

class MySexyForm(Form):
    def __init__(self, *args, **kwargs):
        super(MySexyForm, self).__init__(*args, **kwargs)
        for field in self.fields:
            self.fields[field].required = False

Then call:

然后打电话:

form = MySexyForm(...)
form.save()

However you'll need to make sure your clean() method can handle any missing attributes by conditionally checking if they exist in cleaned_data. For example, if another form field validation relies on customer_id but your partial form have not specified one, then customer_id would not be in cleaned_data.

但是,您需要确保clean()方法可以通过有条件地检查clean_data中是否存在任何缺少的属性来处理它们。例如,如果另一个表单字段验证依赖于customer_id但您的部分表单未指定一个,则customer_id将不在clean_data中。

If this is for a model form, you could check if the value was in cleaned_data, and fallback onto instance.field if it was missing, for example;

如果这是用于模型表单,则可以检查该值是否在cleaning_data中,并且如果缺少则返回到instance.field,例如;

def clean(self):
    inst = self.instance
    customer_id_new = self.cleaned_data.get('customer_id', None)
    customer_id_old = getattr(self.instance, 'customer_id') if inst else None
    customer_id = customer_id_new if customer_id_new else customer_id_old

Remember that the value new value will almost certainly not be in the same format as the old value, for example customer_id could actually be a RelatedField on the model instance but an pk int on the form data. Again, you'll need to handle these type differences within your clean.

请记住,值new值几乎肯定不会与旧值的格式相同,例如,customer_id实际上可能是模型实例上的RelatedField,而是表单数据上的pk int。同样,您需要在清洁中处理这些类型差异。

This is one area where Django Forms really are lacking sadly.

这是Django Forms真正缺乏的一个领域。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号