批量处理

class CustomerView(View):
    def get(self,request):
        if reverse('customer_list') == request.path:
            customer_list = models.Customer.objects.all()
        elif reverse('mycustomers') == request.path:
            customer_list = models.Customer.objects.filter(consultant=request.user).all()
        return render(request, 'customer_list.html', {'customer_list': customer_list})

    def post(self,request):
        print(request.POST)
        func_str=request.POST.get('action')
        data=request.POST.getlist('selected_pk_list')
        if not hasattr(self,func_str):
            return HttpResponse("非法输入")
        else:
            func=getattr(self,func_str)
            func(data)
            ret=self.get(request)
            return ret

    def patch_delete(self,data):
        models.Customer.objects.filter(pk__in=data).update(gender=2)



urls
 url(r'^customers/list/', views.CustomerView.as_view(), name='customer_list'),
    url(r'^mycustomers/', views.CustomerView.as_view(), name='mycustomers'),
{% extends 'base/main.html' %}

{% block main %}
    <form action="" method="post">
    <ol class="breadcrumb">
        <select name="action" id="" class="form-control" style="display: inline-block;width: 250px">
            <option value="patch_delete">delete selected data</option>
        </select>
        <button style="vertical-align: 0px" class="btn btn-danger">GO</button>
    </ol>
    <div class="col-xs-12 row">
        <table class="text-center table table-bordered table-hover">
        <thead>
        <tr>
            <th><input type="checkbox"></th>
            <th>编号</th>
            <th>客户姓名</th>
            <th>性别</th>
            <th>QQ</th>
            <th>当前薪资</th>
            <th>当前状态</th>
            <th>咨询日期</th>
            <th>客户来源</th>
            <th>销售</th>
            <th>所报班级</th>
        </tr>
        </thead>
        <tbody>
        {% for customer in customer_list %}
            <tr>
                <td><input type="checkbox" name="selected_pk_list" value="{{ customer.pk }}"></td>
                <td>{{ forloop.counter }}</td>
                <td>{{ customer.name }}</td>
                <td>{{ customer.get_gender_display }}</td>
                <td>{{ customer.qq }}</td>
                <td>{{ customer.salary }}</td>
                <td>{{ customer.get_status }}</td>
                <td>{{ customer.date|date:'Y-m-d' }}</td>
                <td>{{ customer.get_source_display }}</td>
                <td>{{ customer.consultant }}</td>
                <td>{{ customer.get_classlist }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    </div>

    </form>

{% endblock %}
class Customer(models.Model):
    """
    客户表
    """
    qq = models.CharField(verbose_name='qq', max_length=64, unique=True, help_text='QQ号必须唯一')

    name = models.CharField(verbose_name='学生姓名', max_length=16)
    gender_choices = ((1, '男'), (2, '女'))
    gender = models.SmallIntegerField(verbose_name='性别', choices=gender_choices)

    education_choices = (
        (1, '重点大学'),
        (2, '普通本科'),
        (3, '独立院校'),
        (4, '民办本科'),
        (5, '大专'),
        (6, '民办专科'),
        (7, '高中'),
        (8, '其他')
    )
    education = models.IntegerField(verbose_name='学历', choices=education_choices, blank=True, null=True, )
    graduation_school = models.CharField(verbose_name='毕业学校', max_length=64, blank=True, null=True)
    major = models.CharField(verbose_name='所学专业', max_length=64, blank=True, null=True)

    experience_choices = [
        (1, '在校生'),
        (2, '应届毕业'),
        (3, '半年以内'),
        (4, '半年至一年'),
        (5, '一年至三年'),
        (6, '三年至五年'),
        (7, '五年以上'),
    ]
    experience = models.IntegerField(verbose_name='工作经验', blank=True, null=True, choices=experience_choices)
    work_status_choices = [
        (1, '在职'),
        (2, '无业')
    ]
    work_status = models.IntegerField(verbose_name="职业状态", choices=work_status_choices, default=1, blank=True,
                                      null=True)
    company = models.CharField(verbose_name="目前就职公司", max_length=64, blank=True, null=True)
    salary = models.CharField(verbose_name="当前薪资", max_length=64, blank=True, null=True)

    source_choices = [
        (1, "qq群"),
        (2, "内部转介绍"),
        (3, "官方网站"),
        (4, "百度推广"),
        (5, "360推广"),
        (6, "搜狗推广"),
        (7, "腾讯课堂"),
        (8, "广点通"),
        (9, "高校宣讲"),
        (10, "渠道代理"),
        (11, "51cto"),
        (12, "智汇推"),
        (13, "网盟"),
        (14, "DSP"),
        (15, "SEO"),
        (16, "其它"),
    ]
    source = models.SmallIntegerField('客户来源', choices=source_choices, default=1)
    referral_from = models.ForeignKey(
        'self',
        blank=True,
        null=True,
        verbose_name="转介绍自学员",
        help_text="若此客户是转介绍自内部学员,请在此处选择内部学员姓名",
        related_name="internal_referral"
    )
    # course = models.ManyToManyField(verbose_name="咨询课程", to="Course")

    status_choices = [
        (1, "已报名"),
        (2, "未报名")
    ]
    status = models.IntegerField(
        verbose_name="状态",
        choices=status_choices,
        default=2,
        help_text=u"选择客户此时的状态"
    )

    consultant = models.ForeignKey(verbose_name="课程顾问", to='UserInfo', related_name='consultanter',
                                   limit_choices_to={'depart_id': 1001})
    course = MultiSelectField('咨询课程',choices=course_choices)
    date = models.DateField(verbose_name="咨询日期", auto_now_add=True)
    recv_date = models.DateField(verbose_name="当前课程顾问的接单日期", null=True)
    last_consult_date = models.DateField(verbose_name="最后跟进日期", )
    class_list = models.ManyToManyField('ClassList',verbose_name='已报班级')

    def __str__(self):
        return self.name
    def get_classlist(self):
        l=[]
        for cls in self.class_list.all():
            l.append(str(cls))
        return mark_safe('<br>'.join(l))
    def get_status(self):
        status_color={
            1:'green',
            2:'orange'
        }
        return mark_safe("<span style='background-color:%s;color:white'>%s</span>"%(status_color[self.status],self.get_status_display()))
上一篇:浅谈 所有奇数长度子数组的和 问题


下一篇:Django模板层3和ajax初始