Django Save Partial Progress on Form

I have django webapp with several users logging in and filling out a form.

Some users may start filling out the form and not have the necessary data (for example, grant #) needed to validate the form (and before we begin its work). I want them to be able to fill out a form and be able to save partial information (so that another day they can log in and complete it) or send full information to be verified.

I am currently using ModelForm for all the forms I use, and the Model has limitations to provide reliable data (for example, grant # must be unique). However, I want them to be able to store this intermediate data without any validation.

The solution I was thinking seems rather inelegant and un-django-ey: create a “Save partial form” button that saves the POST dictionary, transforms it into a shelf and creates a “SavedPartialForm” model that connects the user with partial forms stored on the shelf . Does this seem reasonable? Is there a better way to save a POST dict directly in db? Or is it an add-on module that does this partial form persistence (which seems to be a fairly common activity with web forms)?

My biggest concern about my method is that I want to finally be able to make this form - automatically autosave (say every 10 minutes) in some ajax / jquery method, without actually clicking a button and not sending a POST request (for example, the user is not redirected from the page when autosave is triggered). I am not familiar with jquery and I am wondering if this can be done.

+6
python django django-forms
source share
4 answers

There is a good solution in Pro Django from Marty Alchin . In a nutshell, you create another model containing a form hash, a form field, and a stored value. When you renew, you simply upload in the form according to this hash.

+5
source share

before saving:

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

then

 form.save() 
+2
source share

The problem is that you have several forms.

Partial Incomplete. Complete. Ready for it. Ready for it.

In fact, you have a form for the workflow stage.

There is nothing wrong.

  • Find out where you are in the workflow.

  • Pour and submit the form for the next step.

Forms can inherit from each other to preserve duplicate validation methods.

+1
source share

Paste the following form into the __init__ form

 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 need to make sure that your clean() method can handle any missing attributes, conditionally checking to see if they exist in cleaned_data. For example, if another form field check is based on customer_id , but your incomplete form did not specify one, then customer_id will not be in cleaned_data.

If this is for the model form, you can check if the value was in cleaned_data , and drop it to instance.field if it is 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 of the new value will almost certainly not be in the same format as the old value, for example customer_id can actually be RelatedField on the model instance, and pk int in the form data. Again, you will need to handle these type differences within your pure.

This is one area where Django Forms is lacking sadly.

0
source share

All Articles