从管理员链接预填写外键?
一些介绍。我有一个“类似行星”的提要聚合器,顶部有一个额外的层。该额外层允许对聚合帖子进行评论和阐述。这是一些代码供参考。
class Post(models.Model):
title = models.CharField()
published = models.DateTimeField()
class Story(models.Model):
title = models.CharField()
post = models.ForiegnKey(Post)
Story
有一个用于 Post
的外键,当我写一个故事时,我从下拉列表中选择一个帖子。现在,几周后,这个列表可能会变得非常不守规矩。我可以使用 raw_id_fields
,但这有点违反直觉,因为我必须找到我需要的帖子的 ID。
编辑:经过一些研究,我删除了我的误导性问题。我想知道这样的事情是否可能(假设 application
是我的...应用程序的名称。
<a href="/admin/application/story/add/?post=[post.id]">Write about this post.</a>
如果这需要更多解释,请告诉我。:)
Some introduction. I have a "planet-like" feed aggregator with an extra layer on top. That extra layer allows for comments and elaboration on the aggregated posts. Here's some code for reference.
class Post(models.Model):
title = models.CharField()
published = models.DateTimeField()
class Story(models.Model):
title = models.CharField()
post = models.ForiegnKey(Post)
Story
has a ForeignKey to Post
and when I write a story I pick a post from the drop-down. Now, after a few weeks the list could get pretty unruly. I could use raw_id_fields
, but that's a bit counter-intuitive since I would have to find the ID of the post I needed.
EDIT: After doing some research, I removed my misleading question. I'm wondering if something like this is possible (given that application
is the name of my... application.
<a href="/admin/application/story/add/?post=[post.id]">Write about this post.</a>
Let me know if THIS needs any more explanation. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来管理员可以识别 GET 值。因此,
将
post
设置为正确的 ID。 :)Looks like the admin recognizes GET values. So,
would set
post
to the proper ID. :)您可能需要考虑使用自动完成字段而不是 raw_id_fields。
Jannis Leidel 有一个很好的解释,并附有示例,关于如何针对像您这样的情况向 djando 管理添加自动完成功能。
您将需要添加 jquery,但过程并不复杂。
You might want to think about using an autocomplete field instead of raw_id_fields.
Jannis Leidel has a good explanation, with examples, on how to add auto-complete functionality to the djando admin exactly for cases like yours.
You will need to add jquery to the mix, but the process is not that complicated.