Django多对多表过滤器会产生太多的SQL查询

ohtdti5x  于 2023-06-25  发布在  Go
关注(0)|答案(1)|浏览(92)

我正在用django_filters过滤我的Department模型。但它提出了太多的疑问。我该怎么解决?

# models.py

class Branch(models.Model):
    name = models.CharField(max_length=100)

    class Meta:
        verbose_name_plural = "Branches"

    def __str__(self):
        return self.name

class Department(models.Model):
    name = models.CharField(max_length=100)
    branch = models.ManyToManyField(Branch, related_name="department")

    def __str__(self):
        return self.name

_

# views.py

class DepartmentListAPIView(generics.ListAPIView):
    queryset = Department.objects.all()
    serializer_class = DepartmentSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_class = DepartmentFilter

_

# serializers.py

class DepartmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Department
        fields = "__all__"

_

# filters.py

class DepartmentFilter(filters.FilterSet):
    branch = filters.CharFilter(field_name="branch__id", lookup_expr="in")

    class Meta:
        model = Department
        fields = "__all__"

pcww981p

pcww981p1#

这与 filtering 无关,而是与 serializing 有关:序列化器强制为每个Department分别获取相关的Branch es,你可以在一个额外的查询中获取所有这些:

class DepartmentListAPIView(generics.ListAPIView):
    queryset = Department.objects.prefetch_related('branch')
    serializer_class = DepartmentSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_class = DepartmentFilter

注意:由于ManyToManyField是指元素的 * 集合 *,因此ManyToManyField通常会被赋予一个 * 复数 * 名称。因此,您可能需要考虑将branch重命名为branches

相关问题