用户抛出的Django multipleechoicefield选择

2024-04-25 08:42:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个表单,允许用户从特定组中选择任意多个用户。但是,当我尝试使用用户列表作为选项时,我得到一个错误,说“User”对象不支持索引。在

它是一个相当标准的表单,主要的区别是组是根据传递给表单的一个kwarg进行过滤的。表单被传递一个project_id(project object primary key),然后它会找到与该项目相关联的组并生成字段。在

从表单.py在

class ModifyTeamForm(forms.Form):

    action = ChoiceField(choices=[('remove', 'Remove users'), ('promote', 'Promote to lead.')])



    def __init__(self, *args, **kwargs):

        # The project to get the team for
        project_id = kwargs.pop('project_id', None)

        super(ModifyTeamForm, self).__init__(*args, **kwargs)

        project = Project.objects.get(pk=project_id)

        # Team for this project
        team = User.objects.filter(groups__name=project.project_name)

        # Create a form field to select current team members
        current_team = MultipleChoiceField(required=True, choices = team, widget=CheckboxSelectMultiple)

        # Add the field
        self.fields['current_team'] = current_team

我的视图.py在

^{pr2}$

Tags: to用户pyselfprojectid表单init