[英]Django Save Incomplete Progress on Form

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.


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 个解决方案



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中有一个很好的解决方案。简而言之,您创建另一个模型,其中包含表单的哈希,表单域和保存的值。恢复时,只需根据它的哈希表格加载。



before Saving:


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





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.




Place the following into your form __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(...)

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.


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;


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-2019 粤ICP备14056181号