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?
Try to override
__init__()
method like this:Also the same result can be achieved with: