Django 管理员 - 创建“临时”入口

发布于 2024-10-18 12:25:00 字数 265 浏览 1 评论 0原文

有没有办法让 Django 管理面板在您单击“添加”按钮时创建一个临时模型?

我希望能够将多个文件/媒体“附加”到特定的模型条目,这将涉及在创建时上传文件。在模型具有 pk 之前我无法执行此操作,因为显然我无法在上传的文件和条目之间创建链接。

我正在使用 Content-Type 框架在我上传的文件(包装在一个类中)之间创建附件,

我注意到,例如,当您单击“新帖子”按钮获取时,Wordpress 会创建所谓的“自动草稿”围绕问题。

Is there a way to make it so the Django Admin panel creates a temporary model when you click the "add" button?

I want to be able to 'attach' multiple files / media to a particular model entry which would involve uploading the files at the time of creation. I can't do this until the model has a pk as obviously I can't create a link between the uploaded file and the entry.

I am using the Content-Type framework to create the attachment between my uploaded file (which is wrapped in a class)

I noticed that Wordpress for example creates what is called an 'auto draft' when you click the "new post" button to get around problem.

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

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

发布评论

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

评论(1

随波逐流 2024-10-25 12:25:00

如果我理解正确的话,您需要注意上传文件的文件名与使用文件字段的模型的 PK 相对应。

您无需在 django-admin 中更改任何内容,而是对模型进行一些调整:

首先,使用 "upload_to" 在你的文件字段中。我通常将文件名设置为 uuid4-value 以确保它是唯一的。

保存模型后,您可以根据需要重命名该文件。最好的地方是由

If I understand correctly, you want to take care that the filename of your uploaded file corresponds with the model's PK where the file-fields are used.

There is nothing you must change in the django-admin, but make some adjustions on your model:

First, make use of "upload_to" in your filefield. I usually set the filename to a uuid4-value to make sure it's unique.

After saving the model, you can rename the file if you want. The best place is in a function that is triggered by a post-save-signal. But if you only want to ensure, that the filename is unique, the filename-generation by uuid should work.

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