如何更改外键字段在 django admin 中的显示方式?
我的模型如下所示:
Location:
state
county
city
street
Building:
name
location = ForeignKey(Location)
现在,在管理中,编辑 Building
时,我希望能够以这种方式编辑位置:
所以,它就像一个内联,但在 Building
中有 Location
,而不是相反的方式。
My models look like this:
Location:
state
county
city
street
Building:
name
location = ForeignKey(Location)
Now, in admin, when editing the Building
, I want to have the ability to edit the Location in this way:
So, it's like an inline, but with having Location
in Building
, not the oposite way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在建筑物和位置之间保持一对一的关系,您的问题可能会更容易解决。例如,通过从位置对建筑物进行子类化或将位置字段集成到建筑物中。
我认为没有多少建筑物共享同一位置。因此,无论如何使用位置的外键,您不会节省太多。这个外键也使编辑变得复杂。特别是,如果您想要位置组件的单独输入字段。通常,您首先必须在现有位置中搜索匹配项,然后再创建新的位置条目。
以下示例使 Building 成为 Location 的子类,并将建筑物和位置字段分组到管理表单的两个部分中。不过,您的应用程序可能需要一些微调。
模型:
管理表单:
Your problem is probably easier to solve if you keep a one-to-one relationship between building and location. For example, by subclassing building from location or by integrating the location fields into buildings.
I assume that not many buildings share the same location. So you would not save much using a foreign key to locations anyway. This foreign key also makes editing complicated. In particular, if you want separate entry fields for location components. Normally, you would first have to search existing locations for a match before creating a new location entry.
The following example makes Building a subclass of Location and groups building and location fields into two sections of the admin form. Your application will probably require some fine tuning though.
The model:
The admin form: