父模型一对多中子项的显示列表
我正在学习 django admin,我必须对它们之间的一对多关系进行建模。 我得到了类似制造商模型的东西,我可以在其中添加不同的汽车制造商,以及用于添加汽车的汽车模型。在我的 django 管理页面中,当我单击制造商 1 条目时,我希望能够显示制造商 1 制造的所有汽车的列表。
我发现了一个技巧,在制造商管理模型中使用内联模型,问题是它加载数据库中的每个条目,并且需要一些时间,因为它是一个大表。
还有其他方法可以做到这一点还是我必须创建一个新模板?
编辑: 目标不是像 InlineModelAdmin 那样加载每辆 FK 到Manufacturer1 的汽车,而是获得与 display_list 相同的显示,并将结果拆分为页面
I am learning django admin, i have to models with a one-to-many relation between them.
I got something like Manufacturer model where i can add different car manufacturers, and Car model for adding cars. In my django admin page i want to be able to display_list of all cars manfuctred by say manufacturer1 when i click on manufacturer1 entry.
I have found a trick by using Inline model in manufacturer admin model, the problem is that it loads every entry in the database and it takes some time as it's a big table.
Is there any method else to do that or do i have to create a new template ?
EDIT:
The goal is not to load every Car that is FK to Manufacturer1 like with InlineModelAdmin but to get the same display as with display_list with results split in pages
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答您更新的问题:
方法可以是使用
ProxyModels
和重写ModelAdmin.queryset
您可以通过
FordCar
扩展Car
模型,GMCar
及使用对于这两者,在Meta
类中proxy=True
。然后,您可以为
FordCar
和GMCar
注册单独的管理员,并覆盖每个 ModelAdmin 中的查询集方法以过滤相应的制造商。前任:
Answer for your updated question:
the way to do it could be by using
ProxyModels
and overridingModelAdmin.queryset
You extend the
Car
model viaFordCar
,GMCar
and useproxy=True
in theMeta
class, for both those.Then you can register seperate admins for each of
FordCar
andGMCar
and override the queryset method in each of those ModelAdmin to filter for the respective manufacturer.Ex:
你有两个选择。
最简单的方法是逆向看待这种关系。而不是去制造商改变形式并查看他们所有的汽车。转到汽车变更列表并按制造商过滤。您必须在汽车
ModelAdmin
上设置list_filter
属性以包含制造商。选项二将是一个巨大的痛苦,但您可以覆盖制造商
ModelAdmin
上的change_view
将该制造商的汽车列表添加到extra_context
>。然后,您必须覆盖“templates/admin/yourapp/manufacturer/change_form.html”处的管理模板。然后,您可以添加到该模板,以使用传递到extra_context
的汽车列表生成您正在寻找的列表,借鉴“django/contrib/admin/templates/change_list.html”寻求灵感。仔细阅读有关管理的 Django 文档。里面其实蕴含着丰富的信息。
You have two options.
The easiest approach is to look at the relationship in reverse. Instead of going to a manufacturer change form and seeing all their cars. Go to the cars changelist and filter by manufacturer. You'll have to set the
list_filter
attribute on the carModelAdmin
to include manufacturer.Option two, is going to be a huge pain, but you can override
change_view
on the manufacturerModelAdmin
to add the list of that manufacturer's cars toextra_context
. Then, you'll have to override the admin template at 'templates/admin/yourapp/manufacturer/change_form.html'. You can then add to that template to produce the kind of list you're looking for using the list of cars you passed intoextra_context
, drawing on 'django/contrib/admin/templates/change_list.html' for inspiration.Give the Django docs on the Admin a good thorough read. There's actually a wealth of info in there.
你不需要任何黑客。当您选择Manufacturer1时,Django管理仅显示对Manufacturer1具有FK的汽车,只要您使用了 InlineModelAdmin 正确且符合预期。
You don't need any hack. Django admin displays only Cars that have a FK to Manufacturer1, when you select Manufacturer1, as long as you have used the InlineModelAdmin right and as intended.