Django管理员可以通过related_name处理一对多关系吗? [英] Can Django admin handle a one-to-many relationship via related_name?

查看:309
本文介绍了Django管理员可以通过related_name处理一对多关系吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django管理员通过HTML< SELECT>可以快速地支持多对一和多对多的关系。表单域,允许分别选择一个或多个选项。有一个很好的Javascript filter_horizo​​ntal 小部件来帮助。

The Django admin happily supports many-to-one and many-to-many relationships through an HTML <SELECT> form field, allowing selection of one or many options respectively. There's even a nice Javascript filter_horizontal widget to help.

我正在尝试从一个通过相关名称的多个方面。我看不出与多对多多不同,就像在表单中显示一样,我只需要一个多选择的SELECT列表。但是我不能简单地将 related_name 值添加到我的 ModelAdmin 来源字段列表中。

I'm trying to do the same from the one-to-many side through related_name. I don't see how it's much different from many-to-many as far as displaying it in the form is concerned, I just need a multi-select SELECT list. But I cannot simply add the related_name value to my ModelAdmin-derived field list.

Django是否以这种方式支持一对多字段?

Does Django support one-to-many fields in this way?

我的Django模型类似于此(为简化示例而设计):

My Django model something like this (contrived to simplify the example):

class Person(models.Model):
    ...
    manager = models.ForeignKey('self', related_name='staff',
                                null=True, blank=True, )

从个人管理页面,我可以轻松获得< SELECT>列出所有可能的工作人员,以选择这个人员的经理。我还想显示一个多选< SELECT>所有经理人员的列表。

From the Person admin page, I can easily get a <SELECT> list showing all possible staff to choose this person's manager from. I also want to display a multiple-selection <SELECT> list of all the manager's staff.

我不想使用内联,因为我不想编辑下属的详细信息;我想要能够从列表中添加/删除人员。

I don't want to use inlines, as I don't want to edit the subordinates details; I do want to be able to add/remove people from the list.

(我试图使用 django-ajax选择来替换SELECT小部件,但这是by-by-by。)

(I'm trying to use django-ajax-selects to replace the SELECT widget, but that's by-the-by.)

推荐答案

ManyToManyField的解决方案:
http://code.djangoproject.com/ticket/13369#comment:2

Solution for ManyToManyField: http://code.djangoproject.com/ticket/13369#comment:2

这篇关于Django管理员可以通过related_name处理一对多关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆