Django过滤ManyToManyField

时间:2018-02-14 17:34:59

标签: django serialization model django-rest-framework

我需要在序列化之前过滤ManyToManyField的结果。

示例:

用户只有在&exphu_date'没有通过授权计划'正在申请个人'谁是记录的主题。

我的模型和序列化程序目前显示所有相关授权程序记录的列表。

问题:

如何将该列表过滤为仅过期的记录,然后再将其作为父级'个人列表中的列表返回?记录吗

注意: 这不是基于M2M字段过滤父查询集的问题。它是关于在返回父节点之前过滤M2M字段的内容。

目前,序列化程序只是跳过了'active_authorized_program_list'字段。

class Individual(models.Model):
    name = models.CharField(max_length=128, blank=True, default='')
    dob = models.DateField(null=True)
    authorized_program_list = models.ManyToManyField(
        'Program',
        related_name='individual_authorized_program_list',
        through='ReleaseAuthorization',
        blank=True
    )

    @property
    def current_age(self):
        if self.dob:
            today = datetime.date.today()
            return (today.year - self.dob.year) - int(
                (today.month, today.day) <
                (self.dob.month, self.dob.day))
        else:
            return 'Unknown'

    @property
    def active_authorized_program_list(self):
        return (
            self
                .authorized_program_list
                .get_queryset()
                .filter(expiration_date__lte=datetime.Date())
        )

class Program(models.Model):
    name = models.CharField(max_length=128, blank=True, default='')
    individual_list = models.ManyToManyField(
        'individual.Individual',
        related_name='program_individual_list',
        through='hub_program.ReleaseAuthorization',
        blank=True
    )
    active = models.BooleanField(default=True)

class ReleaseAuthorization(models.Model):
    individual = models.ForeignKey(
        Individual,
        related_name="release_authorization_individual",
        on_delete=models.CASCADE
    )
    program = models.ForeignKey(
        Program,
        related_name="release_authorization_program",
        on_delete=models.CASCADE
    )
    expiration_date = models.DateField()


class IndividualSerializer(serializers.ModelSerializer):
    class Meta:
        model = Individual
        fields = (
            'name',
            'dob',
            'current_age',
            'authorized_program_list',
            'active_authorized_program_list',
        )

序列化结果缺少&#39; active_authorized_program_list&#39;的最后一行:

{
    "name": "Individual One",
    "dob": "1974-10-11",
    "current_age": 43,
    "authorized_program_list": [1,2,3]
}

还应该:

"active_authorized_program_list": [1,2]

1 个答案:

答案 0 :(得分:1)

尝试将序列化程序中的字段类型指定为PrimaryKeyRelatedField

class IndividualSerializer(serializers.ModelSerializer):
    active_authorized_program_list = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

    class Meta:
        model = Individual
        fields = (
            'name',
            'dob',
            'current_age',
            'authorized_program_list',
            'active_authorized_program_list',
        )

您也可以使用SerializerMethodField直接在序列化程序中生成列表:

class IndividualSerializer(serializers.ModelSerializer):
    active_authorized_program_list = SerializerMethodField()

    class Meta:
        model = Individual
        fields = (
            'name',
            'dob',
            'current_age',
            'authorized_program_list',
            'active_authorized_program_list',
        )

    def get_active_authorized_program_list(self, obj):
        return (
            self
            .authorized_program_list
            .filter(expiration_date__lte=datetime.Date()).values_list('id', flat=True)
         )