Home > Mobile >  trying to create a row on a table after making a POST -django
trying to create a row on a table after making a POST -django

Time:02-08

How can I create a new row in table notificacao everytime i make this post? (save is not working i get 'QuerySet' object has no attribute 'save')

def post(self, request):
        cliente1 = Cliente.objects.get(cpf=request.data['cliente1_cpf_transf'])
        cliente2 = Cliente.objects.get(cpf=request.data['cliente2_cpf_transf'])
        notificacao1 = Notificacao.objects.all()

        if cliente1.saldo >= request.data['quantia']:
            cliente1.saldo -= request.data['quantia']
            cliente1.save()

            cliente2.saldo  = request.data['quantia']
            cliente2.save()

            notificacao1.cpf_remetente = request.data['cliente1_cpf_transf']
            notificacao1.cpf_destinatario = request.data['cliente2_cpf_transf']
            notificacao1.valor = request.data['quantia']
            notificacao1.save()

CodePudding user response:

QuerySet is kinda like a list, and you cannot save all objects in list just like that.

You are probably in need to make notificacao1 as new Notificacao object. You need to create it like this:

notificacao1 = Notificacao.objects.create(cpf_remetente=cliente1, cpf_destinatario=cliente2, valor=request.data['quantia'])

Whole suggested code:

   def post(self, request):
        cliente1 = Cliente.objects.get(cpf=request.data['cliente1_cpf_transf'])
        cliente2 = Cliente.objects.get(cpf=request.data['cliente2_cpf_transf'])
        valor = request.data['quantia']
        
        if cliente1.saldo >= valor :
            cliente1.saldo -= valor 
            cliente1.save()

            cliente2.saldo  = valor 
            cliente2.save()

            Notificacao.objects.create(cpf_remetente=cliente1, cpf_destinatario=cliente2, valor=valor)

Because this is calling all objects of model Notificacao:

all_objects_of_notificacao = Notificacao.objects.all()
  •  Tags:  
  • Related