python - ManyToMany Relation in Template (Reverse LookUp) -
i'm trying build own client database , don't reverse lookup work:
models.py
class personqueryset(models.queryset): def employees(self): return self.filter(role='e') class personmanager(models.manager): def get_queryset(self): return personqueryset(self.model, using=self._db) def employees(self): return self.get_queryset().employees() class person(models.model): people = personmanager() role = models.charfield(max_length=1, choices = (('c', _('client')),('e', _('employee')))) class organization(models.model): employees = models.manytomanyfield( employee, limit_choices_to=q(role='e'), related_name='organization_employees', related_query_name='organization_employee',)
views.py
class persondetail(detailview): model = person template_name = 'clients/person_detail.html'
the organization-class relates on manytomany-relation several employees (person-class). detailview (template "person_detail.html") of every employee may display organization, each employee belongs, think person.organization.name
i've tried , many other solutions, has never worked far, don't why i'm stucked.
<ul> {% organization in people.organization_set.all %} {{ organization.name }} {% endfor %} </ul>
many many :)
i don't think can call many reverse lookup queryset, instance of model. use instead:
<ul> {% person in people %} {% organization in person.organization_set.all %} {{ organization.name }} {% endfor %} {% endfor %} </ul>
you add method peoplequeryset
class add functionality.
class personqueryset(models.queryset): def organization_set(self): return organization.objects.filter(pk__in=self.values_list('organization', flat=true))
Comments
Post a Comment