How to learn to represent complicated relationship in Django model? -
consider train ticket booking app. there many cities, stations , trains.
each city has one/more stations. obvious station can not in more 1 city. each station receive one/more train(s). every train visits 1 or more station , stops @ fixed (common) destination.
want make form user able select city, stations in same city , train (which station of city receives) , book train.
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 city") class bookedticket(models.model): city = models.foreignkey(city) station = models.foreignkey(station) train = models.foreignkey(train) please suggest on how establish relationships because not able display correct stations chosen city , similarly, train chosen city , station.
you use _set find stations in city
my_city = city.objects.get(name="my hometown") stations_in_city = my_city.station_set.all()
Comments
Post a Comment