1

DjangoQuerySet.update不处理transaction.commit_manually, 并自动提交。

我像这样构建了一个自定义的 django 视图......

类Foo(查看)

@method_decorator(transaction.commit_manually)
def post(self, request, *args, **kwargs):
    try:
        Model.objects.all().update(rank=10)
        data_to_update = resquest.
        for d in data_to_update:
            model = Model.objects.get(pk=d['id'])
            model.rank = d['rank']
            model.save()
        transaction.commit()
        status_code = 200
    except Exception, e:
        # do some stuff
        transaction.rollback()
        status_code = 500

    return HttpResponse(content='Foo', status=status_code)

Model.objects.all().update(rank=10)...如果 在transaction.rollback()_
为每个人调用 save 方法(性能原因)。

4

0 回答 0