我们要想实现多选,首先想到的肯定是select 的多选框,当然你可以用一些插件实现input框中多选和删除,看你自己的选择,这里我用的select的多选

循环ServiceInfo表中field得到类似下面的效果:

我们先看看model层

class CustomerInfo(models.Model):
	user =  models.ForeignKey(User)
	service_info = models.CharField('产品',max_length=256,help_text='产品')  //存入数据格式id|id|id|等
class ServiceInfo(models.Model):
    name = models.CharField('服务名称',max_length=64,help_text="服务名称",unique=True,blank=False,null=False,db_index = True)
    add_time = models.DateTimeField(auto_now_add=True,default=datetime.datetime.now())
    class Meta:
        app_label = 'config' 
        unique_together = (('name'),)
我们的html页面:
 <div class="field">
                <label>产品服务</label>
                <select id="service_ids" size='5' class="select-xlarge" multiple="multiple" name="service_infos" οnclick="check_service()">
                  {% for item in serviceinfos %}
                  <option value="{{item.id}}">{{item.name}}</option>
                  {% endfor %}
                </select>
//将多选的值通过隐藏域传递给后台数据库
<input type="hidden" name="service_info" id="service_info">
javascrip代码:
<script>
function serealizeSelects (select)
      var array = [];
      select.each(function(){ array.push($(this).val()) });
      return array;
function check_service(){
  var service_ids=serealizeSelects($('#service_ids'))
  var service_str=''
    for (var i = service_ids.length - 1; i >= 0; i--) {
      for (var a=0;a<= service_ids[i].length - 1; a++) {
        service_str+=service_ids[i][a]+'|'
console.log(service_str) //1|3|
 $('#service_info').val(service_str)
</script>

我们选择第一个和第三个,得到的service_ids=[[‘1’,‘3’]]

a=['a','b']
console.log(a.push('v')) //将元素插入到数组最后面,然后返回新数组的长度,3

序列后select数组后,将数组元素以|的连字符的方式存入掩藏域中,然后传到后台,存入数据库中

前端显示的话,需要将类似1|3|的值,先劈开,再查询id所对应的name

customerObjects = CustomerInfo.objects.all().order_by("-id")
	for customer in customerObjects:
		service_ids = customer.service_info.split('|')
		serviceinfos = ''
		for id in service_ids:
			if id:
				serviceinfos += ServiceInfo.objects.get(id=id).name+','
		customer.serviceinfos = serviceinfos
我们在前端展示的时候循环customers对象的值时,直接这样输出就好了,所有多选的通过,连接了
           <th>产品线</th>
        </thead>
        <tbody>
          {%for info in customers.object_list %}
            <td>{{info.serviceinfos}}</td>
class Article(models.Model): name = models.CharField('标题', max_length=256) country = models.CharField('国家', max_length=256) province = models.CharField('省份', max_length=2 author_list = request.POST.getlist('author_list') ### 转载于:https://www.cnblogs.com/robinunix/p/7435026.html Django 中获取text,password 名字:<input type="text" name="name"><br><br> 密码:<input type="password" name="password">   Form表单提交 数据 时使用的是post方式,所以在后端接收参数的时候需要先判断请求方式为post时才能请求到 数据 name... Django 如何使用多 枚举。 在Djngo orm 的使用过程中经常会遇到使用多 枚举的情况。但 django 提供的models.CharField是单 的。咋办呢?提供两个思路,1、使用一对多,建立多张表实现。 2、第三方插件MultiSelectField库 先上案例: from multiselectfield import MultiSelectField # ... MY_CHOICES = (('item_key1', 'Item title 1.1'), 添加HTML表单 blog\templates\archive.html <form action="/blog/create/" method="post"> {% csrf_token %} Title: <input type=text name=... 本文主要讲解如何获取用户在html页面中输入的信息。1.首先写一个自定义的html网页login.htmltestform表单里的action{%url ‘check"%} 对应的是urls.py里的name值2.配置urls.py文件urlpatterns = [path("reg/",views.reg,name="check"),path("",views.login),]3.配置views... Currently i filter by some option in django 's admin interface. For instance lets say i filter by 'By status'. Is it possible to select multiple statuses to filter results from? Here is the screenshot ... Django 中,html 页面通过 form 标签来传递表单 数据 。对于复 框信息,即 checkbox 类型,点击 submit 后, 数据 将提交至 view 中的函数。我们通过request.POST.get() 函数来获取来自 html 页面的值,但是该函数只能 get 到 中的最后一个值。因此想要传递 中的多个值,需要用 request.POST.getlist() 函数该函数返回一个列表,... 在页面的右上角有一个添加待办事项的按钮,输入内容并点击添加之后,内容就会添加到页面的表里面,成为第5个待办事项。 我们知道,点击按钮之后将会提交一个表单。这个表单就是我们的待办... 自用运维平台的权限系统中有涉及到一个用户管理的功能,其中包含了用户的角色修改,用户和角色是多对多的关系,前端编辑界面的角色展示为了减少更新麻烦,采用动态从后端获取角色列表,这样后端修改也不需要前端去做更新,管理界面的配置用户角色的地方需要用到复 框,因为是多对多,这里用js去完成修改后赋值到初始字段中,使后端可以轻易的 读取 , 前端用户管理界面 点击编辑按钮,弹出对应的用户信息,这里是之前写的如何给子页面赋值 可以看到这里的角色是可以多 的,而且这些 项是动态从后端获取的。 动态获取角色(获取复 框).