Django的两个比较值列表更新?

时间:2019-03-29 03:54:59

标签: django

我想比较两个语句中的值,如果还有其他值,请更新它们。

models.py

class RecommendLaywer(models.Model):
    category_idx = models.IntegerField(primary_key=True)
    lawyer_idx = models.IntegerField(blank=True, null=True)
    register_date = models.DateTimeField()
    update_date = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'recommend_laywer'
        verbose_name_plural = "recommend laywer"

models.py添加

    @csrf_exempt
    def recommend_add(request):

        if request.is_ajax() and request.method == "POST":
            print(request.POST)

            category_list = request.POST.getlist('arr_category[]')
            lawyer_list = request.POST.getlist('arr_lawyer[]')
            print("category_list1=====", category_list)
            print("lawyer_list2=====", lawyer_list)

            for lawyer in lawyer_list:
                for lawyer in lawyer_list:
                    ??????????????????????????

        code = 0
        msg = "TEST."

        data = json.dumps({
            'code': code,
            'msg': msg,
            #'retURL': retURL

        })
        return HttpResponse(data, content_type='application/json')


console print.....

    category_list1===== ['1', '2', '3', '4', '5', '6', '7', '8', '9']
    lawyer_list2===== ['65', '37', '57', '58', '130', '62', '38', '51', '110']

category_list1的值是固定的。

1=65
2=37
3=57
4=58
..........

改变湖北律师2的值时,我要更新。

1 个答案:

答案 0 :(得分:1)

似乎您想通过发布的数据更新数据库的记录,请尝试以下操作:

from django.http import JsonResponse
from .models import RecommendLawyer


@csrf_exempt
def recommend_add(request):

    if request.is_ajax() and request.method == "POST":
        print(request.POST)    
        category_list = request.POST.getlist("arr_category[]")
        lawyer_list = request.POST.getlist("arr_lawyer[]")
        print("category_list1=====", category_list)
        print("lawyer_list2=====", lawyer_list)
        for category_id, lawyer_id in zip(category_list, lawyer_list):
            RecommendLawyer.objects.filter(category_idx=category_id).update(
                lawyer_idx=lawyer_id
            )

    code = 0
    msg = "TEST."

    data = {
        "code": code,
        "msg": msg,
        # 'retURL': retURL
    }
    return JsonResponse(data)