【问题标题】:ManyToMany Relation in Template (Reverse LookUp)模板中的多对多关系(反向查找)
【发布时间】:2018-01-04 16:27:53
【问题描述】:

我正在尝试构建自己的客户端数据库,但我只是无法进行反向查找:

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'

Organization-Class 通过多对多关系与多个员工(Person-Class)相关联。每个员工的DetailView(模板“person_detail.html”)现在可以显示每个员工所属的组织,我想像person.organization.name这样的东西

我已经尝试过这个和许多其他解决方案,但到目前为止它从未奏效,我只是不明白为什么我被卡住了。

<ul>
{% for organization in people.organization_set.all %}
    {{ organization.name }}
{% endfor %}
</ul>

非常感谢您的帮助:)

【问题讨论】:

  • people 来自哪里?您显示的 PersonDetail 视图不会添加该变量。你的意思是person 吗?
  • 人来自“Person-Class”:people = PersonManager()。我用它来调用额外的 Employee-Class: class Employee(Person) 像这样:people.employee。但我认为在这种情况下我应该使用 person.organization_set.all。你说得对。但这也不行。

标签: python django django-models django-templates django-views


【解决方案1】:

我认为您不能从 QuerySet 调用许多反向查找,只能调用模型的一个实例。你可以改用这个:

<ul>
{% for person in people %}
    {% for organization in person.organization_set.all %}
        {{ organization.name }}
    {% endfor %}
{% endfor %}
</ul>

您还可以向 PeopleQuerySet 类添加一个方法来添加此功能。

class PersonQuerySet(models.QuerySet):
    def organization_set(self):
        return Organization.objects.filter(pk__in=self.values_list('organization', flat=True))

【讨论】:

  • 感谢 ARJMP 的建议。我认为我的代码中的某些内容仍然是错误的。它不工作。 views.py PersonDetail 类可能有问题吗?我尝试添加一个 def get_context_data(self, **kwargs),但这也没有用。但我找到了一个完全不同的解决方案,向 Person(models.Models) 类添加一个新的 def 并使用 connection.cursor() 作为光标执行自定义 sql。可能不太合适,但工作正常:)
【解决方案2】:

如果我将 def 添加到我的 Person(models.Model) 类并执行自定义 sql,我发现了一个不同的解决方案。

models.py

class Person(models.Model):
     def get_organization(self):
        with connection.cursor() as cursor:
            cursor.execute('SELECT organization_id FROM clients_organization_employees WHERE employee_id = %s', [self.id])
            organization_id = cursor.fetchone()
            cursor.execute('SELECT name FROM clients_organization WHERE id = %s', [organization_id[0]])
            organization_name = cursor.fetchone()

        for x in organization_name:
            return x

在模板中我现在可以调用

{{ person.get_organization }}

而且我总是找到合适的组织,每个员工都属于该组织。

【讨论】:

    猜你喜欢
    • 2016-09-15
    • 2019-05-18
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 2014-03-29
    • 2015-11-16
    相关资源
    最近更新 更多