I cannot update information in the bank

Asked

Viewed 40 times

-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'),
]

2 answers

0

Hello.

One idea is to recover the values on a... update.html page

I would create a function that would pick up by id

from django.shortcuts import render

def updateReservar(request,reservar_id):

   reservar =   NumeroForm.objects.get(id=reservar_id)
   formularioReservar = NumeroForm(request.POST)
   if formularioReserva.is_valid():
    preparar = formulario.data
    ...... 
    # codigo para salvar as informações recuperadas
  return render(request, "updateReservar.html", {'reservar': reservar})

in the py.

path('reservaID/<reservar_id>', views.updateReservar, name='reservaID')

or

 path('update/<int:id>',views.updateReservar,name = "update")

html update:

{{reservar.reservar}}

In the browser url would look like this

localhost:8000/reservaID/1

localhost:8000/reservaID/2

localhost:8000/reservaID/3

recovers the values and when appearing in the edit field, in is_valid() adds a save to save the modified information.

your model is with the same name as Forms.

Numeroform model

Numeroform Forms

my code writing may look different but the idea is the same

0


I appreciate the help, but I managed to solve the problem like this(see codes). How I can receive 1 or 999 modal numbers (each number is separated by a comma), plus name and phone.

If there is any suggestion of improvement, I accept

There was only change in views.py.

py views.

def reservar(request):
    numeros = request.POST['numero']
    num = numeros.split(",")
    for n in num:
        num = Numero.objects.get(numero=n)
        num.reservado_por = request.POST['reservado_por']
        num.telefone = request.POST['telefone']
        num.status = 'R'
        num.save()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.