-1
I am developing a customer registration project/ addresses/ products, and now I am doing the inventory part (list which products certain clients hired), and I put to send the customer ID and product, but the problem is time to check whether the customer/product exists before creating. In fact, he even checks to see if they exist, but he creates them in the database in the same way. This way I did, it does the POST, but puts in place the ID "ID not found", I wonder if there is any way I interrupt this post, it send some error message and not create in the database My models:
from django.db import models
import requests
class Base(models.Model):
criado = models.DateField('Data de criação', auto_now_add=True)
class Meta:
abstract = True
class catalogo(Base):
produto = models.CharField('Produto', max_length=50,null=False, blank=False, unique=True)
preco = models.FloatField('Preço (R$)',null=True,blank=True, default=0)
disponibilidadepacote = models.BooleanField('Disponível:',null=False,blank=False,default=True)
def __str__(self):
return self.produto
class pacotesContratados(Base):
produto = models.IntegerField(blank=False, null=False)
cliente = models.IntegerField(blank=False, null=False)
@property
def cliente_infos(self):
url_cliente = "http://localhost:8000/clientes/" + str(self.cliente) + "/"
resposta = requests.get(url_cliente, auth=('joel.neto','123'))
resposta_data = resposta.json()
if resposta.status_code == 200:
return resposta_data
else:
return "Não existe cadastro para esse ID!"
Don’t write Solved in the title. To give a question as closed accept an answer, if the answer does not solve the problem wait for another answer or write a.
– Augusto Vasques