Django rest framework Django REST:排除某些字段并过滤嵌套序列化程序

Django rest framework Django REST:排除某些字段并过滤嵌套序列化程序,django-rest-framework,Django Rest Framework,我试图从约会序列化程序中排除字段“prospect”,它嵌套在单元序列化程序下 如下所示,由于约会查询集上的一些数据操作,我决定使用SerializerMethodField来定义约会序列化程序。但是,我不确定在使用SerializerMethodField时如何排除字段 或者,我可以为约会使用ModelSerializer,它允许我定义要包含哪些字段,但这样我就无法以我想要的方式操作数据 记下要做的事情 型号 class Appointment(models.Model): appoi

我试图从约会序列化程序中排除字段“prospect”,它嵌套在单元序列化程序下

如下所示,由于约会查询集上的一些数据操作,我决定使用SerializerMethodField来定义约会序列化程序。但是,我不确定在使用SerializerMethodField时如何排除字段

或者,我可以为约会使用ModelSerializer,它允许我定义要包含哪些字段,但这样我就无法以我想要的方式操作数据

记下要做的事情

型号

class Appointment(models.Model):
    appointment_time = models.DateTimeField()
    unit = models.ForeignKey(Unit, on_delete=models.CASCADE)
    staff = models.ForeignKey(Staff, on_delete=models.CASCADE)
    prospect = models.ForeignKey(Prospect, on_delete=models.CASCADE)
class UnitSerializer(serializers.ModelSerializer):
    availability = SerializerMethodField()
    manager = ManagerSerializer()
    instruction = InstructionSerializer(source='instruction_set', many=True)
    appointment = SerializerMethodField()

    class Meta:
        model = Unit
        fields = ['id', 'address', 'manager', 'availability', 'instruction', 'appointment']

    def get_availability(self, instance): 
        queryset = instance.availability_set.order_by('start_time')
        return AvailabilitySerializer(queryset, many=True).data

    def get_appointment(self, instance):
        start_buffer = 0.5 ## How many hours from now do we start displaying appointments in the queue
        end_buffer = 72 ## How many hours from now do we stop displaying appointments in the queue
        start_cutoff = datetime.now() + timedelta(hours=start_buffer)
        end_cutoff = datetime.now() + timedelta(hours=end_buffer)
        queryset = instance.appointment_set.exclude(appointment_time__lt=start_cutoff).exclude(appointment_time__gt=end_cutoff).order_by('appointment_time')
        return AppointmentSerializer(queryset, many=True).data
序列化程序

class Appointment(models.Model):
    appointment_time = models.DateTimeField()
    unit = models.ForeignKey(Unit, on_delete=models.CASCADE)
    staff = models.ForeignKey(Staff, on_delete=models.CASCADE)
    prospect = models.ForeignKey(Prospect, on_delete=models.CASCADE)
class UnitSerializer(serializers.ModelSerializer):
    availability = SerializerMethodField()
    manager = ManagerSerializer()
    instruction = InstructionSerializer(source='instruction_set', many=True)
    appointment = SerializerMethodField()

    class Meta:
        model = Unit
        fields = ['id', 'address', 'manager', 'availability', 'instruction', 'appointment']

    def get_availability(self, instance): 
        queryset = instance.availability_set.order_by('start_time')
        return AvailabilitySerializer(queryset, many=True).data

    def get_appointment(self, instance):
        start_buffer = 0.5 ## How many hours from now do we start displaying appointments in the queue
        end_buffer = 72 ## How many hours from now do we stop displaying appointments in the queue
        start_cutoff = datetime.now() + timedelta(hours=start_buffer)
        end_cutoff = datetime.now() + timedelta(hours=end_buffer)
        queryset = instance.appointment_set.exclude(appointment_time__lt=start_cutoff).exclude(appointment_time__gt=end_cutoff).order_by('appointment_time')
        return AppointmentSerializer(queryset, many=True).data
JSON对象表示法


只需创建一个特定的
AppointmentSerializer
(如
UNITAppointSerializer
,其中排除了这些字段)。您可以从原始序列化程序中进行子类化,从而生成一个修改的序列化程序,但将原始序列化程序作为“基”@Willem Van Onsem但我如何修改
AppointmentSerializer
中的数据?我正在考虑创建一个名为filtered_Appointment的新字段作为SerializerMethodField(),然后在Appointment模型下编写过滤方法。这有意义吗?在这里,由于您首先过滤查询集,所以进行更改看起来很奇怪。但我认为在这里最好使用一个亚串行化器,而不是一种方法:好吧,经过更多的研究,我发现了如何解决这个问题。我想你指的是使用“子类”。在本例中,我对ListSerializer进行了子类化,并重写了to_表示方法。在本次讨论中得到了很好的解释: