Field match, referenced in previous field

Asked

Viewed 92 times

1

I started learning about Python from Jango and am trying to develop a simple vehicle management system. I am beating head to perform the following procedure: as soon as the registration plate is entered he return me in the field medida_de_saida the value of the odometer registered in the vehicle.

1

from django.db import models
from veiculos.models import Veiculos
from motoristas.models import Motoristas
from veic_dados.models import Secretarias


class Retirada(models.Model):
    veiculo = models.ForeignKey(Veiculos, on_delete=models.CASCADE)
    motorista = models.ForeignKey(Motoristas, on_delete=models.CASCADE)
    secretaria = models.ForeignKey(Secretarias, on_delete=models.CASCADE)
    data_retirada = models.DateTimeField()
    ultima_medida = queryset = Veiculos.objects.all().only('medida_inicial')
    medida_de_saida = models.CharField(max_length=6)
    destino = models.TextField()
    passageiros = models.TextField()

    def __str__(self):
        return self.veiculo


  [1]: https://i.stack.imgur.com/46UAA.png

4 answers

0

In the case medida_de_saida will always be a fixed value and you will never edit?

If this is the case you can use an Override the save() method from Django.

class Retirada(models.Model):
veiculo = models.ForeignKey(Veiculos, on_delete=models.CASCADE)
motorista = models.ForeignKey(Motoristas, on_delete=models.CASCADE)
secretaria = models.ForeignKey(Secretarias, on_delete=models.CASCADE)
data_retirada = models.DateTimeField()
ultima_medida = queryset = Veiculos.objects.all().only('medida_inicial')
medida_de_saida = models.CharField(max_length=6, editable=False)
destino = models.TextField()
passageiros = models.TextField()

def save(self, *args, **kwargs):
    self.medida_de_saida = 'LOGICA PARA CALCULAR ULTIMA MEDIDA AQUI'
    super(Retirada, self).save()

def __str__(self):
    return self.veiculo

See if you understand, and if I understand correctly your doubt!

  • Exact in the 'ultima_medida' will always be a fixed value that is registered in 'vehicle' when the plate is typed it shows the final km of this vehicle. This 'LOGIC TO CALCULATE LAST MEASURE HERE' would be?

  • Where I inserted the 'LOGIC .... ' is just you put the logic to calculate it. This field will not be editable and every time you save the template automatically and will perform your logic and save it in the database, and you can access it normally. See if you can, and I’m here to try and help!

  • I made a correction, the right thing is self measured, for example if the output measure is always the last measure multiplied by two. That line would be self.measure_de_output = self.ultima_measure * 2 ! I couldn’t add more because I don’t know anything about the business rule.

  • self.medida_de_saida = Vehicles.objects.get(id=self.veiculo_id). hodometro_value .

  • Your self is instantiated wrong, the output measure will receive a value and its Editable must be false in the field declaration. The rest seems to be right if this is the data you want to access from another table.

0

Django has "signal dispatcher" (Signals) Some signals made available:

  • Django.db.models.signals.pre_delete
  • Django.db.models.signals.post_delete
  • Django.db.models.signals.pre_save
  • Django.db.models.signals.post_save

To gain access to some value before new value is saved in the database, you can use Django.db.models.signals.pre_save, with signature: receiver(Sender, instance, raw, using, update_fields) The instance field has the updated data. But as it has not yet been saved in bank of bados, any recovery consultation you make, like: Retirada.objects.get(pk=instance.pk) return the data not updated.

Excellent article to help you: How to Create Django Signals

0

I put it this way, but the last measured field, still not appearing.

from django.db import models
from veiculos.models import Veiculos
from motoristas.models import Motoristas
from veic_dados.models import Secretarias


class Retirada(models.Model):
veiculo = models.ForeignKey(Veiculos, on_delete=models.CASCADE)
motorista = models.ForeignKey(Motoristas, on_delete=models.CASCADE)
secretaria = models.ForeignKey(Secretarias, on_delete=models.CASCADE)
data_retirada = models.DateTimeField()
ultima_medida = queryset = Veiculos.objects.filter().only('medida_inicial')
medida_de_saida = models.CharField(max_length=6)
destino = models.TextField()
passageiros = models.TextField()

def save(self, *args, **kwargs):
    self.ultima_medida = Veiculos.objects.get(id=self.veiculo).medida_inicial
    super(Retirada, self).save()

def __str__(self):
    return self.veiculo

0

I changed the code and the field 'ultima_medida' appeared, but the last measure reported to the selected vehicle does not appear. Instead appears the same information from the 'vehicle' field'

inserir a descrição da imagem aqui

from django.db import models
from veiculos.models import Veiculos
from motoristas.models import Motoristas
from veic_dados.models import Secretarias


class Retirada(models.Model):
veiculo = models.ForeignKey(Veiculos, on_delete=models.CASCADE)
motorista = models.ForeignKey(Motoristas, on_delete=models.CASCADE)
secretaria = models.ForeignKey(Secretarias, on_delete=models.CASCADE)
data_retirada = models.DateTimeField()
ultima_medida = models.ForeignKey(Veiculos, on_delete=models.CASCADE, related_name='Ultima_Medida')
medida_de_saida = models.CharField(max_length=6)
destino = models.TextField()
passageiros = models.TextField()


def __str__(self):
    return self.veiculo


def save(self, *args, **kwargs):
    self.ultima_medida = Veiculos.objects.get(id=self.veiculo).medida_inicial
    super(Retirada, self).save()

Browser other questions tagged

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