Click the Save and change Boolean Django 2.0 button

2

Gentlemen, I'm studying Django 2.0, and I'm having a problem, when I click Save I want to change the Boolean database from False to True in the database, and I do not the slightest idea how to do it.

If someone can help me?

Follow summary code:

models.py

from django.db import models

class Espera(models.Model):

   ...

   cancelaEspera = models.BooleanField(default=False,  blank=True)
   obsCancelamentoEspera = models.TextField('Observações', null=True, blank=True)

   def __str__(self):
    return "%s %s" % (self.dataEspera)

form.py

from django.forms import ModelForm
from . import models

...

class cancelaEsperaForm(ModelForm):
class Meta:
    model = models.Espera
    fields = ['obsCancelamentoEspera']

view.py

from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from . import models, forms, urls

...

def cancelarEspera(request, id):
    cancela = get_object_or_404(models.Espera, pk=id)
    form = forms.cancelaEsperaForm(request.POST or None, request.FILES or None, instance=cancela)

    if form.is_valid():
        form.save()      
        return redirect('listaEspera')

    return render(request, 'espera//cancelaEspera.html', {'form': form})

template.html

{% extends 'base.html' %}

{% block main %}

    <div class="card">
        <form method="POST">
            {% csrf_token %}

            <h1> Cancelamento da Espera </h1>

            <p> Qual o motivo do Cancelamento? </p>    

            {{ form|bootstrap }}                        

            <h2> Tem certeza que deseja cancelar essa espera?</h2>

            <button type="submit" > Sim </button>
            <a href="{% url 'listaEspera' %}" method="get" > Não </a>       

        </form>
    </div>

{% endblock %}
    
asked by anonymous 18.08.2018 / 16:51

1 answer

0

If you just want to change the value of the BooleanField field, change this way:

if form.is_valid():
    form_ = form.save(commit=False)
    form_.cancela = True
    form_.save()
    return redirect('listaEspera')
    
27.08.2018 / 20:22