title: Public Transportation Webapp in DJANGO : models (many-to-many relationship) for transportation systems and routes
body: <p>This webapp would allow users to create alerts about controlers in public transportation in a big city. It would include mostly trains and buses.</p>
  <p>I'm quite stuck with how to create the model. I've already created the Alert that has a station attribute with a one-to-one relationship, and a line attribute also.
  I have two models : a model Stations, and a model Line.</p>
  <p>Now each transportation mode (bus/train) has a line, stations and a schedule.Each line has several stations (or bus stops), and each stations receives many lines. I used a many-to-many fields in the Line model, but I don't know how to order the stations, since a bus will go through each of its stations in an orderly fashion, neither how to link that to a schedule. I thought about making another model &quot;Line_Station&quot; with each instance having the attributes Line, Station and Order, but that doesn't seem optimal for routes that have many stations, since a Route would be each instance of Line_Station for the same attribute Line. I'm new at Django and haven't really had the chance to manipulate the database relationships, but I feel like this problem could be solved with a many-to-many relationship.</p>
  <p>(Transportations/models.py)</p>
  <pre><code>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)
  
  
  class Line_station(models.Model):
      line = models.ForeignKey(Line, on_delete=models.CASCADE)
      station = models.ForeignKey(Station, on_delete=models.CASCADE)
      order = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(60)])
  
  </code></pre>
  <p>Is there a more optimal way to resolve this ? The point is that I would like to create lines in my django admin and from there, add stations and select the order. If I were to think that this app would attract many users, I feel like creating two more models (routes and schedules) might create too many queries.</p>
  <p>Here's the Alert model if it is of any interest :</p>
  <p>(Alerts/models.py)</p>
  <pre><code>class Alert(models.Model):
      alert_whistleblower = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
      alert_timestamp = models.DateTimeField(auto_now_add=True)
      alert_station = models.ForeignKey(Station, on_delete=models.CASCADE)
      alert_line = models.ForeignKey(Line, on_delete=models.CASCADE)
      alert_remarks = models.TextField(blank=True, null=True)
  
      def get_absolute_url(self):
          return reverse('alerts:detail', kwargs={'pk':self.pk})
  </code></pre>
  <p>Here's a diagram of the models, but I don't really know how to connect Schedule to route, since the model Route wouldn't be &quot;one route&quot; but many instances (one route would be each instance of the same line attribute, and an order).</p>
  <p><a href="https://i.stack.imgur.com/Ct6Oy.png" rel="nofollow noreferrer">Diagram</a></p>
  <p>How can I make my model more optimized ?</p>