全部)。我使用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
。我想要最少数量的数据库请求,并且没有更好的方法:使用raw / execute或其他django-orm方法?
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)
答案 0 :(得分:0)
我只是使用联接表,而使用manytomany却没有什么好处,那么您只需查询联接
class Department(models.Model):
"""Model that represents department information."""
title = models.CharField(max_length=256)
class DepartmentCollaboratorJoin(models.Model):
"""Model that represents join."""
department = models.ForeignKey("Department")
collaborator = models.ForeignKey("Collaborator")
joininfo = 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)
答案 1 :(得分:0)
我找到了解决方案- prefetch_related 。它仅调用两个查询,就足够了。
onFileSelected(event) {
if(event.target.files.length > 0)
{
console.log(event.target.files[0].name);
}
}
因此,其他过滤器将由前端完成。