Interrupt the POST Django

Asked

Viewed 34 times

-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.

1 answer

0

Why don’t you care about your customer’s model and product in this view? By the way, I’m guessing there’s a model of them, you didn’t actually specify...

from clientes import Cliente
from produto import Produto

That way, instead of you using an Integerfield, you use a Foreign Key:

I’m not sure I understand the logic of your @Property and the relationship of it to what you said about saving or not saving at the base. I think the question is poorly formulated and lacking code to be understood.

But I would alter it so she would check in that way:

class pacotesContratados(Base):
    produto = models.ForeignKey(Produto , blank=False, null=False)
    cliente = models.ForeignKey(Cliente ,blank=False, null=False)

    @property
    def cliente_infos(self):
        cliente = Cliente.objects.filter(cliente=self.cliente)

        if cliente:
           // tem cliente, faz alguma coisa
        else:
           // nao tem cliente, faz outra coisa

It seems to me that it would be more appropriate to be in a view actually...

  • thanks for the reply, I managed to solve by doing a data validation function in the serializer and forgot to deactivate here this way q vc commented would be possible if it were all in the same project, but in my case were two different Apis, one of customers and one of products (containing the above code), my problem was time to take the data from the other API and do the check when I have time put here some resolution of this anyway thanks for the time

Browser other questions tagged

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