3

I'm trying to implement a simple "checkpointing" system to save partially completed formsets. I've got a set of large forms (say 100 entries) for a data-entry project. Now, if the person quits or whatever, halfway through, then I'd like this progress saved - but I don't want the half-entered data saved in the database until it's complete.

As far as I can see the best way to deal with this is to save request.POST to a database field and pull it out again e.g.

 def myview(request, obj_id):
     obj = get_object_or_404(Task, obj_id)
     if request.POST:
         # save checkpoint
         obj.checkpoint = serializers.serialize("json", request.POST)
     else:
         # load last version from database.
         request.POST = serializers.deserialize("json", obj.checkpoint)
     formset = MyFormSet(request.POST) 
     # etc.

But, this gives me the following error:

'unicode' object has no attribute '_meta'

I've tried simple json and pickle and get the same errors. Is there any way around this?

Ahmad Azwar Anas
  • 1,289
  • 13
  • 22
Puzzled79
  • 1,037
  • 2
  • 10
  • 23

1 Answers1

2

Django's serializer interface works with django model objects. It will not work with other objects.

You may try to use json

if request.POST:
     # save checkpoint
     obj.checkpoint = json.dumps(request.POST)
     post_data = request.POST
else:
     # load last version from database.
     post_data = json.loads(obj.checkpoint)

formset = MyFormSet(post_data)
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • Doesn't work - I get the following error: AttributeError: 'dict' object has no attribute 'getlist' – Puzzled79 Apr 29 '13 at 10:02
  • @Simon7, Django's `request.POST` is not a simple dict, but a `QueryDict`. So it may give errors while using saved data. To create querydict from dict refer http://stackoverflow.com/questions/13363628/django-can-i-create-a-querydict-from-a-dictionary – Rohan Apr 29 '13 at 11:40