models.py
class Box(models.Model):
length = models.IntegerField(null=False)
width = models.IntegerField(null=False)
height = models.IntegerField(null=False)
class BoxColorHistory(models.Model):
box = models.ForeignKey(Box, related_name='color_history')
start_datetime = models.DateTimeField(null=False)
end_datetime = models.DateTimeField(null=True)
color = models.CharField(max_length=20)
I would like to write a query that returns all Boxes that have current color='green'.
The problem is that the color is constantly changing. Each time the color changes, this is recorded in the BoxColorHistory table.
The current color of the box is the row in the BoxColorHistory table with the most recent start time and an end time of null.