【发布时间】:2011-11-26 05:58:05
【问题描述】:
我有两个模型如下:
System_Contact
first_name
last_name
isOwner = CharField ('Y'/'N')
isMainContact = CharField ('Y'/'N')
System
mainContact = ForeignKey(System_Contact)
owner = ForeignKey(System_Contact)
billTo = ForeignKey(System_Contact)
因此,当我在网页中显示System 表单时,用户可以从下拉菜单中选择mainContact owner 和billTo 联系人以保存到System 模型。但是,我想过滤System 表单中的选择字段,使它们像这样:
mainContact Select box: -- only show System_Contacts that have isMainContact = 'Y'
owner Select Box: -- only show Syste_Contacts that have isOwner = 'Y'
现在,我知道如何通过过滤查询集来限制选择框,但我不知道如何过滤相关的外键查询集。由于mainContact 和owner 字段是外键,我需要过滤外表(System_Contact),而不是构建表单的表(System)
我知道如何过滤一个普通的、非外键类型的选择框如下:
form.fields["some_field"].queryset = Some_Model.objects.filter(some_field="Foo")
我将如何“扩展”它以过滤外部表?
这是我目前正在尝试的,但没有成功:
form.fields["mainContact"].queryset = System_Contact.objects.filter(isMainContact = 'Y')
谢谢
【问题讨论】:
标签: django filtering django-queryset