title: Adding a related object as a context value back to a rendered view in Django
body: <p>I have a query I can run in a shell and get the right answer given my models and the way the relations work:</p>
  
  <pre><code># And let's see each business's employees:
  employees=Employee.objects.filter(business_locations__business=business).distinct()
  </code></pre>
  
  <p>This works great, I can get set that to employees and have all the employees that work for a business.  The trouble is I am not sure a clean way to figure this out when trying to actually use this model and render a list view from it.  If you see the list.html code (last snippet) you can see where I am trying to provide the number of employees working for a business in the list and then ideally provide a link to edit the employees using a <a href="http://incrtcl.sourceforge.net/iwidgets/iwidgets/disjointlistbox.gif" rel="nofollow noreferrer">&lt;-- --> arrow widget thingy</a>.</p>
  
  <p>I know I have to do this in the view right below, but I wasn't sure the cleanest way.  Best I could come up with involved a for loop and a map of ids? </p>
  
  <p>I have a view method: in my views.py:</p>
  
  <pre><code>def business_list(request):
      object_list = Business.objects.all()
      paginator = Paginator(object_list, 25) # 3 posts in each page
      page = request.GET.get('page')
      #make a map into the object list some how so I can have this data at the end...
  
  
      try:
          business = paginator.page(page)
      except PageNotAnInteger:
          # If page is not an integer deliver the first page
          business = paginator.page(1)
      except EmptyPage:
          # If page is out of range deliver last page of results
          business = paginator.page(paginator.num_pages)
  
      return render(request, 'ipaswdb/group/list.html', {'page':page, 'business':business, 'object_list':object_list})
  </code></pre>
  
  <p>Then my render view template for that view is this list.html:</p>
  
  <pre><code>{% extends "ipaswdb/base.html" %}
  
  {% block title %}Businesses{% endblock %}
  
  {% block content %}
  
      &lt;h1&gt;Businesses&lt;/h1&gt; &lt;p&gt; Search: &lt;/p&gt;
  
      {% with object_list.count as total_businesses %}
              &lt;h2&gt;
           There are {{ total_businesses }}  businesses
              &lt;/h2&gt;
      {% endwith %}
      &lt;table class="table"&gt;
      &lt;tr&gt;
          &lt;th&gt; ID&lt;/th&gt;
          &lt;th&gt; Business Name&lt;/th&gt;
          &lt;th&gt; Comments&lt;/th&gt;
          &lt;th&gt; Total employees&lt;/th&gt;
          &lt;th&gt;Edit&lt;/th&gt;
          &lt;th&gt;Term&lt;/th&gt;
  
      {% for group in groups %}
          &lt;tr&gt;
              &lt;td&gt;&lt;a href="{{ business.id }}"&gt;{{ business.id }}&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;&lt;a href="{{ business.id }}"&gt;{{ business.business_name }}&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt; {{ business.notes|truncatewords:30|linebreaks }}&lt;/td&gt;
  &lt;!-- How do i do this? --&gt;
              &lt;td&gt;&lt;a href="linktoaddremoveemployeestoabusiness"&gt; {{ business.business_total }}&lt;/a&gt;&lt;/td&gt; 
              &lt;td&gt; editbutton&lt;/td&gt;
              &lt;td&gt;termbutton&lt;/td&gt;
  
          &lt;/tr&gt;
      {% endfor %}
      &lt;/table&gt;
      {% include "pagination.html" with page=groups %}
  
  {% endblock %}
  </code></pre>
  
  <p>Models:</p>
  
  <pre><code>class Employee(models.Model):
          first_name = models.CharField(max_length = 50)
          business_locations = models.ManyToManyField('BusinessLocations', through='EmployeeLocations')
  
  
  class EmployeeLocations(models.Model):
      employee = models.ForeignKey('Employee', on_delete=models.CASCADE)
      business_location = models.ForeignKey('BusinessLocations', on_delete=models.CASCADE)
      created_at=models.DateField(auto_now_add=True)
      updated_at=models.DateField(auto_now=True)
      def __str__(self):
          return self.provider.first_name
  
  class BusinessLocations(models.Model):
      address = models.ForeignKey('Address', on_delete= models.SET_NULL, null=True)
      business = models.ForeignKey('Business', on_delete=models.CASCADE)
      doing_business_as = models.CharField(max_length = 255)
      created_at=models.DateField(auto_now_add=True)
      updated_at=models.DateField(auto_now=True)
      def __str__(self):
          return self.doing_business_as
  
  class Business(models.Model):
      business_name = models.CharField(max_length=50)
  </code></pre>
  
  <p>I had help with these from this stackoverflow question here:
  <a href="https://stackoverflow.com/questions/38233245/giving-a-model-knowledge-of-a-many-to-many-related-model-in-django/38234987?noredirect=1#comment63938527_38234987">Giving a model knowledge of a many-to-many related model in django</a></p>