【发布时间】:2018-07-25 18:26:24
【问题描述】:
全部)。我使用 Django 2.0 编写项目。我有 2 个模型:合作者和部门(1 个合作者可以在一个或多个部门工作)。所以,我想查询如下表:
| | Department 1 | Department 2 | Department 3 |
|:---------------|:------------:|:------------:|:------------:|
| Collaborator 1 | True | False | False |
| Collaborator 2 | True | False | True |
| Collaborator 3 | True | True | True |
- 协作者包含
first_name和last_name。 - 如果合作者在这个部门工作,则为真。
- 如果合作者不在该部门工作,则为 false。
我希望对数据库的请求数量最少,但没有更好的方法:使用 raw/execute 或其他 django-orm 方法?
models.py
class Department(models.Model):
"""Model that represents department information."""
title = models.CharField(max_length=256)
class Collaborator(models.Model):
"""Model that represents collaborator information."""
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
...
departments = models.ManyToManyField(Department)
【问题讨论】:
标签: sql django django-orm