Say I have three models that represent a football team, a football team event, and an event location. Each football team has multiple events. Not every event has a location.
class FootballTeam(models.Model):
team_name = models.CharField()
def get_latest_location(self):
# ???
class Event(models.Model):
team = models.ForeignKey(FootballTeam)
time = models.DateField()
class EventLocation(models.Model):
event = models.ForeignKey(Event)
location_name = models.CharField()
How do I write a Django query to retrieve the latest EventLocation of a FootballTeam? In other words, how do I sort a set of models according to the date of a parent model?
def get_latest_location(self):
return EventLocation.objects.filter(event__team=self).order_by("-event__time").first()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With