使用带有form.fields.queryset的?

时间:2022-01-13 15:46:29

Is it possible to set a form's ForeignKey field's queryset so that it will take separate queryset's and output them in <optgroup>'s?

是否可以设置表单的ForeignKey字段的查询集,以便它将采用单独的查询集并在中输出它们?

Here is what I have:

这是我有的:

views.py

form = TemplateFormBasic(initial={'template': digest.template.id})
form.fields['template'].queryset = Template.objects.filter(Q(default=1) | Q(user=request.user)).order_by('name')

In my Template model, I have default Templates and User-created templates. I want them to be visibly separated in the <select> box eg.

在我的模板模型中,我有默认模板和用户创建的模板。我希望它们在

<select>
  <optgroup label="Default Templates">
    <option>Default 1</option>
    <option>Default 2</option>
  </optgroup>
  <optgroup label="User Templates">
    <option>User Template 1</option>
    <option>User Template 2</option>
  </optgroup>
</select>

Can this be done?

可以这样做吗?

2 个解决方案

#1


10  

I was able to figure it out using the example given on this blog

我能够使用此博客上给出的示例来解决这个问题

views.py

form.fields['template'].choices = templates_as_choices(request)

def templates_as_choices(request):
    templates = []
    default = []
    user = []
    for template in Template.objects.filter(default=1).order_by('name'):
        default.append([template.id, template.name])

    for template in Template.objects.filter(user=request.user).order_by('name'):
        user.append([template.id, template.name])

    templates.append(['Default Templates', default])
    templates.append(['User Templates', user])

    return templates

#2


4  

I've done in the past by not using a foreign key on the form, but rather a charfield with choices.

我过去没有在表单上使用外键,而是选择了一个字段。

A CharField with choices support optgroups. You need to have the choices in this format:

具有选项的CharField支持optgroups。您需要以这种格式进行选择:

('Group 1',(('1','Yada'),('2','Yada'))), ('Group 2',(('3','Bepety'),('4','Bopity')))

('第1组',(('1','Yada'),('2','Yada'))),('第2组',(('3','Bepety'),('4' 'Bopity')))

Choices can also be a callable. So I created my own function that traverses the models and builds a tuple like the above.

选择也可以是可赎回的。所以我创建了自己的函数,遍历模型并构建如上所述的元组。

#1


10  

I was able to figure it out using the example given on this blog

我能够使用此博客上给出的示例来解决这个问题

views.py

form.fields['template'].choices = templates_as_choices(request)

def templates_as_choices(request):
    templates = []
    default = []
    user = []
    for template in Template.objects.filter(default=1).order_by('name'):
        default.append([template.id, template.name])

    for template in Template.objects.filter(user=request.user).order_by('name'):
        user.append([template.id, template.name])

    templates.append(['Default Templates', default])
    templates.append(['User Templates', user])

    return templates

#2


4  

I've done in the past by not using a foreign key on the form, but rather a charfield with choices.

我过去没有在表单上使用外键,而是选择了一个字段。

A CharField with choices support optgroups. You need to have the choices in this format:

具有选项的CharField支持optgroups。您需要以这种格式进行选择:

('Group 1',(('1','Yada'),('2','Yada'))), ('Group 2',(('3','Bepety'),('4','Bopity')))

('第1组',(('1','Yada'),('2','Yada'))),('第2组',(('3','Bepety'),('4' 'Bopity')))

Choices can also be a callable. So I created my own function that traverses the models and builds a tuple like the above.

选择也可以是可赎回的。所以我创建了自己的函数,遍历模型并构建如上所述的元组。