class Company(models.Model):
title = models.CharField()
class Office(models.Model):
сompany = models.ForeignKey(Company, on_delete=models.CASCADE)
title = models.CharField()
class Human(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
office = models.ManyToManyField(Office)
如何在不使用Prefetch的情况下找到当前用户的公司。因为它很贵
2条答案
按热度按时间gjmwrych1#
你可以这样尝试:
这里我通过
Company --> Office --> Human --> User
从Company反向查询User。更多信息可以在文档中找到。ogq8wdun2#
Office.objects.filter(humans__user=request.user).select_related('с ompany')