Hi everybody !
I’m what you could call a “newbie”. I started learning python a few weeks back, and django with Corey Schaffer and the official tutorial on Django Docs (‘polls’).
Now I have a problem with a project that would allow users to create alerts about controlers in public transportation in a big city (such as busses, trains, etc.). The thing that I want to do is the following : if someone sees a controler on the train, he can just create an alert with the corresponding train number (or line), and the station where he was controlled.
Now each station can receive many trains (lines), and each train (line) goes through several stations with an order (all the stations covered by a train would make a route). I also want to implement a schedule, but I have to finish my model to make it work on top of route.
So here’s what I’ve done :
class Station(models.Model): Station_name = models.CharField(max_length=200) Station_adress = models.CharField(max_length=300) Station_vehicule = models.ForeignKey(Vehicule, on_delete=models.CASCADE) class Line(models.Model): Line_number = models.PositiveIntegerField(validators=[MinValueValidator(1), MaxValueValidator(100)]) Line_name = models.CharField(max_length=200, blank=True, null=True) Line_vehicule = models.ForeignKey(Vehicule, on_delete=models.CASCADE) Line_stations = models.ManyToManyField(Station, through='Routes') class Route(models.Model): line = models.ForeignKey(Line, on_delete=models.CASCADE) station = models.ForeignKey(Station, on_delete=models.CASCADE) order = models.IntegerField()
But a “route” is just each instance of the model Route binding Line and Stations. I would like to be able to create routes and add stations, and put the order. I created the attribute order = IntegerField, but I’d also like it to increment automatically for a given line. Is this the most optimal way to do that ? Am I going completely off track ? If this is correct, how could I implement a schedule /timetable to the routes, so that each train can go through the same route several times a day ?