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?

4

1 回答 1

2

Django 的序列化接口与 django 模型对象一起工作。它不适用于其他对象。

您可以尝试使用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)
于 2013-04-29T05:15:50.327 回答