Mรณdulo 09 ยท Django APIs & Auth โ Lecciรณn 1 de 3 ยท ~8 min
POST with models & forms
A booking needs a walker
So far Walker has been the only model in playground/django-pawwalk/. But a booking doesn't exist on its own โ it's always a booking of a specific walker. That's a relationship, and Django models relationships with a ForeignKey.
class Booking(models.Model):
walker = models.ForeignKey("Walker", on_delete=models.CASCADE)
duration_minutes = models.IntegerField()
price_cents = models.IntegerField()Read on_delete=models.CASCADE as an instruction, not decoration: if this walker is deleted, delete their bookings too. Every ForeignKey in Django must say what happens on the other side when the parent row disappears โ there's no silent orphaning.