-1
I receive from a form the following information, number (which must be reserved), reserved_by and phone. I have to take the number to reserve and update it with the information received, but of error. When I try to book a number that is not yet registered in the bank, it works correctly.
When I try to update an existing record in the database error occurs and does not work, but when a new single record works correctly, can help me?
py.models
from django.db import models
class Numero(models.Model):
situacao = (
("D", "Disponivel"),
("R", "Reservado"),
("P", "Pago")
)
numero = models.CharField(max_length=10, unique=True)
reservado_por = models.CharField(max_length=50, blank=True)
telefone = models.CharField(max_length=20, blank=True)
criado_em = models.DateField(auto_now_add=True)
status = models.CharField(max_length=1, choices=situacao, null=False, blank=False)
class Meta:
verbose_name = "número"
verbose_name_plural = "números"
def __str__(self):
return self.numero
Forms.py
from django import forms
from apps.numeros.models import Numero
class NumeroForm(forms.ModelForm):
class Meta:
model = Numero
fields = ('reservado_por', 'telefone', 'numero')
py views.
from django.shortcuts import render
from apps.numeros.models import Numero
from apps.rifas.models import Rifa
from .forms import NumeroForm
from django.urls import reverse_lazy
def reservar(request):
if request.method == 'POST':
form = NumeroForm(request.POST)
if form.is_valid():
numero = form.save(commit=False)
numero.status = 'R'
numero.reservado_por = form.cleaned_data['reservado_por']
numero.telefone = form.cleaned_data['telefone']
numero.save()
reverse_lazy('home')
py.
from django.urls import path
from .views import home, numero, rifa, reservar
urlpatterns = [
path('', home, name='home'),
path('numero/', numero, name='numero'),
path('rifa/', rifa, name='rifa'),
path('reservar/', reservar, name='reservar_numero'),
]