Update in a specific field in Django 2.0

Asked

Viewed 350 times

0

I have the following Model:

class Venda(models.Model):
    id = models.AutoField(u'AÇAIEX', primary_key=True)
    hora_saida = models.TimeField(max_length=6)
    responsavel_frete = models.CharField(max_length=14, verbose_name=u'Resp. Frete', choices = RESPONSAVEL_FRETE, default='REMETENTE')
    empresa = models.ForeignKey(Empresa, on_delete=models.CASCADE)
    localidade_origem = models.ForeignKey(Localidade, on_delete=models.CASCADE, verbose_name=u'Loc. Origem', related_name ='localidade_origem')
    localidade_destino = models.ForeignKey(Localidade, on_delete=models.CASCADE, verbose_name=u'Loc. Destino', related_name ='localidade_destino')  
    cliente_origem = models.ForeignKey(Cliente, on_delete=models.CASCADE, related_name ='cliente_origem')
    cliente_destino = models.ForeignKey(Cliente, on_delete=models.CASCADE, related_name ='cliente_destino')
    carro = models.ForeignKey(Carro, on_delete=models.CASCADE)
    motorista_principal = models.ForeignKey(Motorista, on_delete=models.CASCADE, related_name ='motorista_principal')
    motorista_reserva = models.ForeignKey(Motorista, on_delete=models.CASCADE , related_name ='motorista_reserva',  null=True, blank=True)
    #valores default
    data_venda = models.DateField(auto_now_add=True, verbose_name=u'Data',)
    situacao_venda = models.CharField(max_length=10, verbose_name=u'Situação', choices = SITUACAO_VENDA, default='ATIVA')  
    #aba de valores teste
    tipo_frete = models.CharField(max_length=10, choices = TIPO_FRETE, default='PAGO')
    dinheiro = models.BooleanField()
    cartao = models.BooleanField()
    cartoes = models.CharField(max_length=30, choices = CARTOES, default='DINNER CLUBS', null=True, blank=True)      

    #valor_nota = models.DecimalField(verbose_name=u'Valor Nota',
    #                            max_digits=15, decimal_places=2, null=True, blank=True)
    valor_dinheiro = models.DecimalField(verbose_name=u'Valor Dinheiro',
                                 max_digits=15, decimal_places=2,  default=Decimal('0.00'))
    valor_cartao = models.DecimalField(verbose_name=u'Valor Cartão',
                                 max_digits=15, decimal_places=2, default=Decimal('0.00'))
    usuario = models.ForeignKey(User, on_delete=models.CASCADE)
    agencia = models.ForeignKey(Group, on_delete=models.CASCADE, null=True, blank=True)

I wish to update only the field situac_sell, I used the method below:

def save(self, *args, **kwargs):
         venda.save(update_fields=["situacao_venda"])

That did not work, the system still accuses that fields to update.

1 answer

2

What you did was change the method You need to load the desired record, for example the ID 3:

minha_venda = Venda.objects.get(id=3)

From there, you can change the fields you want:

minha_venda.valor_dinheiro = 10.99

And then update the data in the database:

minha_venda.save()
  • is why I made the following script: def save(self, *args, **kwargs): sale = Sale.objects.filter(id=self.id). update(situaca_venda=self.situacao_venda). More has a problem the system only accepts save if I add an inline again because I use inline in admin.

Browser other questions tagged

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