将数据发布到 django 管理表单

发布于 2024-10-12 06:40:36 字数 717 浏览 3 评论 0原文

我正在编写一个 Django 应用程序,其数据将来自不同的来源,包括 Excel 电子表格。我已经编写了一些代码来从这些工作表中进行初始导入,但我不喜欢每次添加行时重新导入整个数据的想法 - 而且我的客户不想自己重新输入数据。

一种可能的解决方案是添加一个带有简单文本区域的表单,用户可以在其中复制粘贴电子表格的整行。然后,视图可以分割数据,对其进行预处理并将其发布到相应对象的标准管理表单。

理想情况下,它的行为就像用户确实从该表单中发布一样:如果数据有效,则创建对象,如果没有,则(管理)表单将重新显示,并带有漂亮的红色错误框。

所以我想我只需要类似的东西

from django.shortcuts import redirect
[...]
return redirect(to, method=POST, *args)

,但它似乎不可用。

我还考虑将数据作为大查询字符串传递,例如 http://.../admin/app/object/add/?ID=1234&name=toto&...但是我的模型有很多字段,一对一许多和多对多内联,可能很长的文本字段等,因此这种方法似乎比必要的更麻烦。

知道如何获取 POST 重定向之类的东西吗?或者解决这个问题的另一种方法?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝颜夕 2024-10-19 06:40:36

如果您已经在编写特定于您的表单的代码,为什么不在同一函数中创建对象,而不是尝试向管理站点伪造 POST 请求呢?

对我来说,使用默认管理表单听起来比使用现有的预处理视图来开始创建或更新对象更困难。

我只需通过 get_urls 方法,设置一个可以简单的模板

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

并手动处理输入表单中的数据 < code>request.POST.get('data', '').split(',') (或者你有什么)并开始填充你的模型。

完成后,发送消息并重定向回您的应用程序视图或更改列表视图。

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

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('../')
梦幻的心爱 2024-10-19 06:40:36

为此,您应该远离内置管理界面。

创建您自己的 ModelForm http://docs.djangoproject .com/en/dev/topics/forms/modelforms/

创建您自己的执行验证和 POST 的视图函数。

这可能应该是一个两步事务。

view_function_1

  • 如果方法是 GET,则显示空表单。

  • 如果方法是 POST,他们会将一个值粘贴到文本框中并填写“其他”字段。

    解析文本框中的数据。

    对于空字段,请填写文本框中缺少的值。

    将表单的数据放入会话中。

    重定向到将移动到 view_function_2 的 URL

view_function_2

  • 如果方法是 GET,则从会话中获取表单数据,填写内容并
    呈现带有数据的表单。

  • 如果方法是POST,验证并保存结果。

    重定向到向用户显示详细信息的页面。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文