Consider a train ticket booking app. There are many cities, stations and trains.
Each city has one/more stations. It is obvious that a station can not be in more than one city. Each station receive one/more train(s). Every train visits one or more station and finally stops at a fixed (common) destination.
Now I want to make a form where the user is able to select his city, stations in the same city and then the train (which that station of the city receives) and finally book the train.
This is what I have done till now.
class City(models.Model):
name = models.CharField(max_length=50, help_text="Your city name")
class Train(models.Model):
number = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50, help_text="Train")
city = models.ForeignKey(City)
class Station(models.Model):
city = models.ForeignKey(City)
name = models.CharField(max_length=50, help_text="All available stations in your city")
class BookedTicket(models.Model):
city = models.ForeignKey(City)
station = models.ForeignKey(Station)
train = models.ForeignKey(Train)
Please suggest on how to establish relationships properly because I am not able to display correct stations for a chosen city and similarly, train for a chosen city and station.