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.
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
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?
– Jaelcio
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!
– Enderson Menezes
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.
– Enderson Menezes
self.medida_de_saida = Vehicles.objects.get(id=self.veiculo_id). hodometro_value .
– Enderson Menezes
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.
– Enderson Menezes