DRF serializer change field to read_only for some

2019-07-15 01:07发布

问题:

I have this model:

class Task(MPTTModel, TimeStampedModel, StartFinishModel):
    name = models.CharField(max_length=256)
    parent = TreeForeignKey('self',
                            on_delete=models.CASCADE,
                            related_name='children')
    start_date = models.DateField()    
    finish_date = models.DateField()
    @property
    def is_stage(self):
        if self.get_children():
            return True
        return False

And serializer:

class TaskBaseSerializer(StartFinishSerializer, TimeStampedSerializer):
    class Meta:
        model = Task
        fields = ('id', 'name', 'parent', 'start_date', 'finish_date', 'is_stage')    
        read_only_fields = ('is_stage')

I want to make fields 'start_date' and 'finish_date' to become read_only if is_stage property is True. How can I do that?

回答1:

Try to override __init__() method like this:

class TaskBaseSerializer(StartFinishSerializer, TimeStampedSerializer):
    class Meta:
        model = Task
        fields = ('id', 'name', 'parent', 'start_date', 'finish_date', 'is_stage')    
        read_only_fields = ('is_stage')

    def __init__(self, *args, **kwargs):
        super(TaskBaseSerializer, self).__init__(*args, **kwargs)
        if self.instance and getattr(self.instance, 'is_stage', None):
            self.fields['start_date'].read_only = True
            self.fields['finish_date'].read_only = True


回答2:

Also the same result can be achieved with:

def get_fields(self):
    fields = super(TaskBaseSerializer, self).get_fields()
    if self.instance and getattr(self.instance, 'is_stage', None):
        fields['start_date'].read_only = True
        fields['finish_date'].read_only = True
    return fields