我正在用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__"
1条答案
按热度按时间pcww981p1#
这与 filtering 无关,而是与 serializing 有关:序列化器强制为每个
Department
分别获取相关的Branch
es,你可以在一个额外的查询中获取所有这些:注意:由于
ManyToManyField
是指元素的 * 集合 *,因此ManyToManyField
通常会被赋予一个 * 复数 * 名称。因此,您可能需要考虑将branch
重命名为branches
。