当该模型中的布尔字段设置为true时,增加一个lnteger字段

时间:2018-02-17 07:04:44

标签: p

class Book(models.Model):
    borrowed = models.BooleanField(default=False)

borrow_count = models.PositiveIntegerField(default=0)

1 个答案:

答案 0 :(得分:0)

也许你应该在视图中这样做:

def book_borrow_status_change(request):
    if request.method == "POST":
        form = BorrowStatusChangeForm(request.POST)
        if form.is_valid():
            book_id = form.cleaned_data['book_id']
            borrowed = form.cleaned_data['borrowed']

            book = get_object_or_404(Book, pk=book_id)

            book.borrowed = borrowed
            book.borrow_count = book.borrow_count + 1
            book.save()
        else:
            return HttpResponse("Form is not valid")
        return HttpResponseRedirect(reverse_lazy('book'))