title: Django - filtering by user
body: <p>I want to render data from the Route model that belongs to the Driver in their 'accounts' page - so displaying the leave_from, destination etc data they have saved in the database so far.</p>
  
  <p>Models.py:</p>
  
  <pre><code>class Driver(models.Model):
      user = models.OneToOneField(User, default=1)
      first_name = models.CharField(max_length=120, blank=True, null=True)
      last_name = models.CharField(max_length=120, blank=True, null=True)
      tel = models.CharField(max_length=120, blank=True, null=True)
      slug = models.SlugField(max_length=120, unique=True)
      timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
      updated = models.DateTimeField(auto_now_add=False, auto_now=True)
  
      def __str__(self):
          return self.user.username
  
      def get_absolute_url(self):
          return reverse("account", kwargs={"slug": self.slug})
  
      def save(self, *args, **kwargs):
          self.slug = slugify(self.first_name)
          super(Driver, self).save(*args, **kwargs)
  
  class Route(models.Model):
      leave_from = models.CharField(max_length=120, blank=True, null=True)
      destination = models.CharField(max_length=120, blank=True, null=True)
      date = models.DateField(auto_now_add=False, auto_now=False)
      time = models.TimeField(auto_now_add=False, auto_now=False)
      driver = models.ForeignKey(Driver, on_delete=models.CASCADE)
  
      def __str__(self):
          return self.leave_from
  </code></pre>
  
  <p>I've played with various querysets and the below is the closest to getting there (I think... I'm new to coding and Django). </p>
  
  <p>Views.py:</p>
  
  <pre><code>def route(request, slug):
      routedetails = Driver.objects.filter(route=request.user.driver.route_set.all())
      context = {
          "routedetails": routedetails,
          }
      return render(request, "route.html", context)
  </code></pre>
  
  <p>With that I am able to get user to display the same number of instances of data in Route for that Driver.</p>
  
  <p>Template:</p>
  
  <pre><code>    {% for route in routedetails %}
          &lt;p&gt;{{ route.user }}&lt;/p&gt;
      {% endfor %}
  </code></pre>
  
  <p>I've tried all different variations but I feel this has got me the closest as it is at least returning the user the same number of times there is data in Route for this user.  In this case there are 2 routes saved in Route and so the username is returned twice.  I have tested on other users and it always matches.</p>
  
  <p>I've looked everywhere and this is as far as I've been able to get so appreciate any help.</p>