使用管理界面中其他模型的值填充 django-textfield 的最佳(=最简单)方法是什么?
我正在为自定义博客 Django 项目开发一个小型新闻通讯应用程序(仅适合我)。该项目的一个主要特点是定义了一组文章类型。所有文章类型都是抽象基类“Article”的子类。文章类型的两个示例是“事件文章”和“视频文章”。在新闻通讯应用程序中,我有一个“内容”字段(=电子邮件消息)。现在我想选择几篇文章(任何类型)包含在时事通讯中。如果我只创建一个函数来搜索新闻通讯中尚未出现的所有文章,可能会更容易。然后我会收集所有需要的信息,将它们组合成文本并将该函数设置为该字段的默认值。但我宁愿自己选择文章。我考虑过 m2m 字段,但如何选择一些文章(内嵌在对象的编辑表单中)并立即在内容字段中填充所需的信息(如绝对 URL 或标题)?提前感谢您的帮助。
I am working on a small newsletter-app for a custom-Blog-Django-project (just for me). One main feature of the project is the defined set of Article-Types. All Article-types are children of the abstract base class "Article". Two examples of article-types are "event-article" and "video-article". In the newsletter-app I have a "content"-Field (=email-message). Now I want to choose several articles (of any type) to be included in the newsletter. It may be easier if I just create a function that searches all articles which are not featured in a newsletter yet. Then I would collect all needed information, combine them into a text and set the function as default for the field. But I rather choose the articles by myself. I thought about a m2m-field, but how can I choose some articles (inline in the edit-form of the object) and have the content-field filled with the needed information (like absolute_url or headline) immediately? Thanks for your help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
唯一的选择是 AJAX。设置一个以 JSON 形式返回文章的视图。当用户选择一篇文章时,对该视图进行 AJAX 调用。然后,使用 JavaScript 解析返回的 JSON 并填充文本字段。
views.py
script.js(为了简单起见,使用 jQuery)
显然,您必须对其进行一些调整,但这是基本过程。
Only option is AJAX. Setup a view that returns an article as JSON. Make the AJAX call to that view when user selects an article. Then, parse the returned JSON with and populate the text field with JavaScript.
views.py
script.js (using jQuery for simplicity)
You'll obviously have to tweak that somewhat, but that's the basic process.