django 子对象中的外键筛选器

li9yvcax  于 2022-11-26  发布在  Go
关注(0)|答案(1)|浏览(102)

我有模型:

class Boss(models.Model):
    fullname = models.TextField()

class Shop(models.Model):
    name = models.TextField()
    address = models.TextField()
    phone = models.TextField()
    boss = models.ForeignKey(
        Boss, on_delete=models.CASCADE, related_name="shops"
    )

class Employee(models.Model):
    name = models.TextField()
    phone = models.TextField()
    shop = models.ForeignKey(
        Shop, on_delete=models.CASCADE, related_name="employees"
    )
class WorkSpace(models.Model):
    name = models.TextField()
    employee = models.ForeignKey(
        Shop, on_delete=models.CASCADE, related_name="work_spaces"
    )

序列化程式:

class WorkSpaceSerializer(serializers.ModelSerializer):
    class Meta:
        model = WorkSpace
        fields = ["id","type"]

class EmployeeSerializer(serializers.ModelSerializer):
    work_spaces = WorkSpaceSerializer(many=True, read_only=True)

    class Meta:
        model = Employee
        fields = ["id","work_spaces","name","phone"]

class ShopSerializer(serializers.ModelSerializer):
    employees = EmployeeSerializer(many=True, read_only=True)

    class Meta:
        model = Shop
        fields = ["id","employees","phone","name","address"]

class BossSerializer(serializers.ModelSerializer):
    shops = ShopSerializer(many=True, read_only=True)

    class Meta:
        model = Boss
        fields = ["id","fullname","shops"]

检视

class BossListView(
    ListModelMixin,
    GenericViewSet,
):
    
    queryset = Boss.objects.all()
    serializer_class = BossSerializer
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        workspace_type = self.request.query_params.get("workspace_type")
        qs = super().get_queryset()
        if workspace_type:
            qs = qs.filter(shops__employees__work_spaces__type=workspace_type).distinct()
        return qs

我用Boss.objects.filter(shops__employees__work_spaces__type=C1)过滤,得到:

{
    "shops": [
        {
            "id": 32,
            "name": "Garden flowers",
            "address": "5 st. Hool-Maa",
            "phone": "879124851861598",
            "employees": [
                {
                    "id": 150,
                    "name": "Mike",
                    "phone": "8154451246",
                    "work_spaces": [
                        {
                            "id": 497,
                            "type": "B12"
                        },
                        {
                            "id": 15,
                            "type": "Z5"
                        },
                        {
                            "id": 33,
                            "type": "C1"
                        }
                    ]
                }
            ]
        }
    ]
}

但我只需要工作空间中的C1:

[{
                  "id": 33,
                  "type": "C1"
                }]

我如何从查询集中排除其他work_spaces,或者我需要将结果转换为列表,然后使用for循环进行筛选吗?可以有许多工作区,我不需要向用户显示所有工作区,我需要关于老板、商店、员工的信息。

mcdcgff0

mcdcgff01#

根据您的观点,您正在workspace_type上应用lower()方法。
因此,如果您有workspace_type=C1,应用lower()将得到workspace_type=c1,并且您的过滤器不会匹配任何记录。
我在这里假设你的值是大写的。

相关问题