开发者

POSTing data to a django admin form

开发者 https://www.devze.com 2023-02-03 16:58 出处:网络
I\'m writing a Django app whose data will be coming from different sources, including Excel spreadsheets. I\'ve written some code to do an initial import from those sheets, but I don\'t like the idea

I'm writing a Django app whose data will be coming from different sources, including Excel spreadsheets. I've written some code to do an initial import from those sheets, but I don't like the idea to re-import the whole data each time a row is added - and my client does not want to re-type the data himself.

A possible solution would be to add a form with a simple textarea where the user could copy-paste a whole line of the spreadsheet. Then a view could split the data, pre-process it and post it to the standard admin form for the corresponding object.

Ideally, it would behave like the user has really posted from this form: if the data validates, the object is created and if not, the (admin) form is re-displayed with the nice red error boxes.

So I thought I would just need something like

from开发者_如何学C django.shortcuts import redirect
[...]
return redirect(to, method=POST, *args)

but it doesn't seem to be available.

I also thought of passing the data as a big query string like http://.../admin/app/object/add/?ID=1234&name=toto&... but my model has many fields, with one-to-many and many-to-many inlines, possibly long textfields, etc. so this approach seems like more trouble than necessary.

Any idea how to obtain something like a POST redirect? or another approach to this problem?


If you're already writing code that is specific to your form, why not create the objects in that same function instead of trying to fake a POST request to the admin site?

To me, it sounds more difficult to use the default admin form than to use your existing pre-processing view to start creating or updating objects.

I'd just hook up your pre-processing view to your ModelAdmin definition via the get_urls method, set up a template that could be as simple as

<form action="." method="post">
    <textarea name="data"></textarea>
    <input type="submit" value="submit" />
</form>

and manually process the data in the input form request.POST.get('data', '').split(',') (or what have you) and start populating your models.

When done, send a message and redirect back to your app view or changelist view.

request.user.message_set.create(message="Finished populating X models")
return http.HttpResponseRedirect('../')


For this, you should step away from the built-in admin interface.

Create your own ModelForm http://docs.djangoproject.com/en/dev/topics/forms/modelforms/

Create your own view functions that does validation and POST.

This should probably be a 2-step transaction.

view_function_1

  • if method is GET, present the empty form.

  • if method is POST, they have pasted a value into the text box and filled in the "other" fields.

    Parse the data in the text box.

    For fields which are empty, fill in the missing values from the text box.

    Put the form's data into the session.

    Do a redirect to a URL that will move to view_function_2

view_function_2

  • If the method is GET, fetch the form data from the session, fill things in and present the form with data.

  • If the method is POST, validate and save the results.

    redirect to a page which will display the details to the user.

0

精彩评论

暂无评论...
验证码 换一张
取 消