父模型一对多中子项的显示列表

发布于 2024-11-06 15:33:51 字数 368 浏览 0 评论 0原文

我正在学习 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 技术交流群。

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

发布评论

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

评论(3

三寸金莲 2024-11-13 15:33:51

回答您更新的问题:

方法可以是使用 ProxyModels 和重写 ModelAdmin.queryset

您可以通过 FordCar 扩展 Car 模型,GMCar及使用对于这两者,在 Meta 类中 proxy=True

然后,您可以为 FordCarGMCar 注册单独的管理员,并覆盖每个 ModelAdmin 中的查询集方法以过滤相应的制造商。

前任:

class FordCarAdmin(admin.ModelAdmin)
    list_display = fields = ['name','model','engine']

    def queryset(self,request):
        qs = super(MyModelAdmin, self).queryset(request)
        return qs.filter(manufacturer__name='Ford')

admin.site.register(FordCar,FordCarAdmin)

Answer for your updated question:

the way to do it could be by using ProxyModels and overriding ModelAdmin.queryset

You extend the Car model via FordCar, GMCar and use proxy=True in the Meta class, for both those.

Then you can register seperate admins for each of FordCar and GMCar and override the queryset method in each of those ModelAdmin to filter for the respective manufacturer.

Ex:

class FordCarAdmin(admin.ModelAdmin)
    list_display = fields = ['name','model','engine']

    def queryset(self,request):
        qs = super(MyModelAdmin, self).queryset(request)
        return qs.filter(manufacturer__name='Ford')

admin.site.register(FordCar,FordCarAdmin)
向地狱狂奔 2024-11-13 15:33:51

你有两个选择。

最简单的方法是逆向看待这种关系。而不是去制造商改变形式并查看他们所有的汽车。转到汽车变更列表并按制造商过滤。您必须在汽车 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 car ModelAdmin to include manufacturer.

Option two, is going to be a huge pain, but you can override change_view on the manufacturer ModelAdmin to add the list of that manufacturer's cars to extra_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 into extra_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.

黯然#的苍凉 2024-11-13 15:33:51

你不需要任何黑客。当您选择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.

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