Django 表单 : MultipleSelect with choices from another model (Foreign Key)

标签 django forms multiple-select

我正在尝试创建一个表单,用户可以在其中选择多个技术人员。当我将行 technician = forms.SelectMultiple(label='Technicians Involved') 添加到我的 forms.py 时,我得到一个没有数据的大空白框。如何使用用户模型中的技术人员填充该框?

models.py

class Incident(models.Model):
    user_id = models.ForeignKey(User, related_name='user')
    technician = models.ForeignKey(User, related_name='technician')
    capa = models.CharField('capa number', max_length=9)

forms.py

class IncidentForm(forms.ModelForm):

    ###################### TRYING! ################################
    technician = forms.SelectMultiple(label='Technicians Involved')
    ###############################################################

    class Meta:
        model = Incident
        fields = [  'user_id',
                    'technician',
                    'capa',
                ]

views.py

def report_incident(request):

    template = "report.html"

    if request.method == 'POST':

        form = IncidentForm(request.POST)

        if form.is_valid():

            # Auto capturing logged in user
            incident = form.save(False)
            incident.user_id = request.user
            incident.save()
            return HttpResponseRedirect('/incidents/')
    else:
        form = IncidentForm() #an unbound form

        return render(request, template, {'form': form})

**************** 更新我所做的更正 ******************** *

models.py

class Incident(models.Model):
    user_id = models.ForeignKey(User, related_name='user')
    technician = models.ManyToManyField(User, related_name='technician')
    capa = models.CharField('capa number', max_length=9)

forms.py

class IncidentForm(forms.ModelForm):

    technician = forms.SelectMultiple()

    class Meta:
        model = Incident
        fields = [  'user_id',
                    'technician',
                    'capa',
                ]

views.py 没有变化

admin.py 进行了更改,以便在管理界面中查看每个事件的多个技术人员。

class IncidentAdmin(admin.ModelAdmin):
        list_display = ('id',
                        'user_id',
                        'capa',
                        'get_technicians'
                        )

       def get_technicians(self):
            return "\n".join([t.technicians for t in obj.technician.all()])

最佳答案

试试这个

class IncidentForm(forms.ModelForm):
   technician = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(), queryset=User.objects.all())

     class Meta:
        model = Incident
        fields = [
                'technician',
                'capa',
               ]

关于Django 表单 : MultipleSelect with choices from another model (Foreign Key),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31970931/

相关文章:

javascript - 如何使用 Highcharts 指定柱形图中相对于 X 轴标签的列位置?

php - 如何使用 PHP 自动填充输入字段并保存 cookie?

forms - 无法在第 91 行的 vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php 中加载类型 "text"

python - Django models.CommaSeparatedIntegerField 与 forms.CheckboxSelectMultiple 小部件

python - 如何在 Django QuerySet 中编写原始 SQL 查询?

python - 使用 Django Rest Framework 返回当前用户

python - 无法使用 MEDIA_ROOT 和 MEDIA_URL 在 Django 中上传图片

javascript - 表单内的 viewChild 不会从原始状态触发它

php - PHP如何处理<select>的多个值?

css - IE9 多选显示问题