相关对象的 Django 查询集
Django queryset of related objects
有以下型号:
class Company(models.Model):
name = models.CharField(max_length=10)
class Department(models.Model):
name = models.CharField(max_length=10)
company = models.ForeignKeyField(to=Company)
persons = models.ManyToManyField(to=Person, on_delete=models.PROTECT)
class Person(models.Model):
name = models.CharField(max_length=10)
我想要queryset
一家公司的所有人
正在使用
def persons_by_company(company_name):
l = []
for d in Department.objects.filter(company__name=company_name):
for p in d.persons:
l.append(p)
return l
会是
- 慢
- return 是列表而不是查询集(不可过滤等)
在这里获取查询集的方法是什么?
首先你的公司或部门必须绑定外键
class Department(models.Model):
name = models.CharField(max_length=10)
company = models.ForeignKeyField(to=Company, related_name="department_company_key")
class Person(models.Model):
name = models.CharField(max_length=10)
person_department = models.ForeignKey(
'Department',
related_name="person_department_key"
on_delete=models.CASCADE,
blank=False,
null=False
)
然后在你的函数中:
def persons_by_company(company_name):
l = []
for d in Department.objects.filter(company__name=company_name):
for p in d.person_department_key.all(): # You also apply some filter()
l.append(p) # Remember This will append object not string or dictionary
return l
不要忘记相关名称必须是唯一的
就我而言,我认为只需要
就很简单
Person.objects.filter(departement__company__id=company_id).distinct()
或公司名称:
Person.objects.filter(departement__company__name__iexact=company_name).distinct()
您的函数将变为:
def persons_by_company(company_name):
<strong>return Person.objects.filter(departement__company__name__iexact=company_name).distinct()</strong>
它 returns 一个 queryset 并且速度更快。我使用 iexact
来避免区分大小写。
更新:
.distinct()
只是为了删除重复的条目。
有以下型号:
class Company(models.Model):
name = models.CharField(max_length=10)
class Department(models.Model):
name = models.CharField(max_length=10)
company = models.ForeignKeyField(to=Company)
persons = models.ManyToManyField(to=Person, on_delete=models.PROTECT)
class Person(models.Model):
name = models.CharField(max_length=10)
我想要queryset
一家公司的所有人
正在使用
def persons_by_company(company_name):
l = []
for d in Department.objects.filter(company__name=company_name):
for p in d.persons:
l.append(p)
return l
会是
- 慢
- return 是列表而不是查询集(不可过滤等)
在这里获取查询集的方法是什么?
首先你的公司或部门必须绑定外键
class Department(models.Model):
name = models.CharField(max_length=10)
company = models.ForeignKeyField(to=Company, related_name="department_company_key")
class Person(models.Model):
name = models.CharField(max_length=10)
person_department = models.ForeignKey(
'Department',
related_name="person_department_key"
on_delete=models.CASCADE,
blank=False,
null=False
)
然后在你的函数中:
def persons_by_company(company_name):
l = []
for d in Department.objects.filter(company__name=company_name):
for p in d.person_department_key.all(): # You also apply some filter()
l.append(p) # Remember This will append object not string or dictionary
return l
不要忘记相关名称必须是唯一的
就我而言,我认为只需要
就很简单Person.objects.filter(departement__company__id=company_id).distinct()
或公司名称:
Person.objects.filter(departement__company__name__iexact=company_name).distinct()
您的函数将变为:
def persons_by_company(company_name):
<strong>return Person.objects.filter(departement__company__name__iexact=company_name).distinct()</strong>
它 returns 一个 queryset 并且速度更快。我使用 iexact
来避免区分大小写。
更新:
.distinct()
只是为了删除重复的条目。