我在选择框中遇到问题,可以在其中选择多个选项,选择的选项在子选择框中有几个对象

时间:2019-06-06 13:33:58

标签: python django django-models django-2.0

首先,我有一个选择框,在这里我必须选择多个选项。选择它们时,代码仅考虑选择的第一个选项,而忽略其余的其他选项。使我无法保存其他选项。

Views.py

def post(self, request):
    user_site = UserSite.objects.get(user_id = request.POST["user_id"])
    user_site.site_id = request.POST['site']
    user_site.save()
    user_site.user.profile.roles = request.POST['role']
    user_site.user.profile.company_id = request.POST['company']
    user_site.user.profile.save()

Only the option of 'Tamrind Valley' is selected, and 'Green Wood High'' is just ignored

html

<select class="form-control select2 edit-company-select" multiple="multiple" name="company" style="width: 100%;" required >
                        <option></option>
                        {% for company in companies %}
                        <option value="{{ company.id }}"{{company.name}}</option>
                        {% endfor %}
                    </select>

1 个答案:

答案 0 :(得分:0)

您可以使用帖子获取列表存储多项选择 例如:

for i in request.POST.getlist('company'):
    company = Company.objects.create(name = i)