У меня следующая ситуация: проект может принадлежать нескольким отделам, сотрудникам назначены роли для нескольких проектов.Как мне показать поле ролей в шаблоне?employee.role показывает пустой ...
МОДЕЛИ
class Projects(models.Model):
name = models.CharField(max_length=20)
class Department(models.Model):
name = models.CharField(max_length=20)
class Employees(models.Model):
fname = models.CharField(max_length=15)
class Departmentprojects(models.Model):
department = models.ForeignKey(Department)
projects = models.ForeignKey(Projects)
members = models.ManyToManyField(Employee, through='Membership')
class Membership(models.Model):
departmentprojects = models.ForeignKey(Departmentprojects)
employee = models.ForeignKey(Employee)
role = models.CharField(max_length=20) #i want to show this field in a template
ПРОСМОТР
def department_detail(request, company_id):
department = get_object_or_404(Department, pk=department_id)
return render_to_response('test/detail.html', {'department': department})
ШАБЛОН
<h1>{{ department.name }}</h1>
{% for projects in department.departmentprojects_set.all %}
<h2>{{ projects.projects }}</h2>
<ul>
{% for employees in projects.members.all %}
<li>{{ employee }} | {{ employee.role }}</li>
{% endfor %}
</ul>
{% endfor %}