Django表单:MultipleSelect,可以从另一个模型中选择(外键) [英] Django Forms: MultipleSelect with choices from another model (Foreign Key)

查看:200
本文介绍了Django表单:MultipleSelect,可以从另一个模型中选择(外键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a form where the user can select multiple technician. When I add the line technician = forms.SelectMultiple(label='Technicians Involved') to my forms.py I get a big blank box with no data. How can I populate that box with the technicians from the User model?

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})

**************** 我在以下进行的更正更新 ******************** *

************** UPDATE WITH CORRECTIONS I MADE BELOW *********************

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 进行了更改,以在管理界面中针对每个事件查看多个技术人员.

admin.py Changes made to view multiple technicians per incident in the admin interface.

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,可以从另一个模型中选择(外键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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