python - Adding a related object as a context value back to a rendered view in Django -


i have query can run in shell , right answer given models , way relations work:

# , let's see each business's employees: employees=employee.objects.filter(business_locations__business=business).distinct() 

this works great, can set employees , have employees work business. trouble not sure clean way figure out when trying use model , render list view it. if see list.html code (last snippet) can see trying provide number of employees working business in list , ideally provide link edit employees using <-- --> arrow widget thingy.

i know have in view right below, wasn't sure cleanest way. best come involved loop , map of ids?

i have view method: in views.py:

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 map object list how can have data @ end...       try:         business = paginator.page(page)     except pagenotaninteger:         # if page not integer deliver first page         business = paginator.page(1)     except emptypage:         # if page 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}) 

then render view template view list.html:

{% extends "ipaswdb/base.html" %}  {% block title %}businesses{% endblock %}  {% block content %}      <h1>businesses</h1> <p> search: </p>      {% object_list.count total_businesses %}             <h2>          there {{ total_businesses }}  businesses             </h2>     {% endwith %}     <table class="table">     <tr>         <th> id</th>         <th> business name</th>         <th> comments</th>         <th> total employees</th>         <th>edit</th>         <th>term</th>      {% group in groups %}         <tr>             <td><a href="{{ business.id }}">{{ business.id }}</a></td>             <td><a href="{{ business.id }}">{{ business.business_name }}</a></td>             <td> {{ business.notes|truncatewords:30|linebreaks }}</td> <!-- how do this? -->             <td><a href="linktoaddremoveemployeestoabusiness"> {{ business.business_total }}</a></td>              <td> editbutton</td>             <td>termbutton</td>          </tr>     {% endfor %}     </table>     {% include "pagination.html" page=groups %}  {% endblock %} 

models:

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) 

i had these stackoverflow question here: giving model knowledge of many-to-many related model in django


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -