Hi,
I am having a headache writing a query
That’s my models:
class Mission(models.Model):
machine = models.ForeignKey(Machine, on_delete=models.CASCADE, related_name="missions")
start_dt = models.DateTimeField(null=False)
longitude = models.DecimalField(max_digits=9, decimal_places=6, null=True)
latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True)
@cached_property
def timezone(self) -> datetime.tzinfo:
timezone_str = get_tz(float(self.longitude), float(self.latitude))
return pytz.timezone(timezone_str)
and
class Machine(models.Model):
name = models.CharField(max_length=12, null=False, unique=True)
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
I want to make a query with all the machines and for each machine a last_mission field where I have my last mission.
like:
machine.last_mission.timezone
...
.I am completely stuck doing it with the django ORM.
that’s my postgres query that doesn’t do exactly what I want as there is no last_mission key and not the model property.
Thanks a lot for you help