class ViewSerializer(serializers.ModelSerializer): type = serializers.SerializerMethodField() table = TableSerializer() filters = ViewFilterSerializer(many=True, source='viewfilter_set') sortings = ViewSortSerializer(many=True, source='viewsort_set') class Meta: model = View fields = ('id', 'name', 'order', 'type', 'table', 'filter_type', 'filters', 'sortings') extra_kwargs = {'id': {'read_only': True}} def __init__(self, *args, **kwargs): include_filters = kwargs.pop( 'filters') if 'filters' in kwargs else False include_sortings = kwargs.pop( 'sortings') if 'sortings' in kwargs else False super().__init__(*args, **kwargs) if not include_filters: self.fields.pop('filters') if not include_sortings: self.fields.pop('sortings') @extend_schema_field(OpenApiTypes.STR) def get_type(self, instance): # It could be that the view related to the instance is already in the context # else we can call the specific_class property to find it. view = self.context.get('instance_type') if not view: view = view_type_registry.get_by_model(instance.specific_class) return view.type
class ViewSerializer(serializers.ModelSerializer): type = serializers.SerializerMethodField() table = TableSerializer() class Meta: model = View fields = ('id', 'name', 'order', 'type', 'table') extra_kwargs = {'id': {'read_only': True}} @extend_schema_field(OpenApiTypes.STR) def get_type(self, instance): # It could be that the view related to the instance is already in the context # else we can call the specific_class property to find it. view = self.context.get('instance_type') if not view: view = view_type_registry.get_by_model(instance.specific_class) return view.type
class ViewSerializer(serializers.ModelSerializer): type = serializers.SerializerMethodField() table = TableSerializer() filters = ViewFilterSerializer(many=True, source='viewfilter_set', required=False) sortings = ViewSortSerializer(many=True, source='viewsort_set', required=False) class Meta: model = View fields = ('id', 'table_id', 'name', 'order', 'type', 'table', 'filter_type', 'filters', 'sortings', 'filters_disabled') extra_kwargs = { 'id': {'read_only': True}, 'table_id': {'read_only': True} } def __init__(self, *args, **kwargs): context = kwargs.setdefault("context", {}) context['include_filters'] = kwargs.pop('filters', False) context['include_sortings'] = kwargs.pop('sortings', False) super().__init__(*args, **kwargs) def to_representation(self, instance): # We remove the fields in to_representation rather than __init__ as otherwise # drf-spectacular will not know that filters and sortings exist as optional # return fields. This way the fields are still dynamic and also show up in the # OpenAPI specification. if not self.context['include_filters']: self.fields.pop('filters', None) if not self.context['include_sortings']: self.fields.pop('sortings', None) return super().to_representation(instance) @extend_schema_field(OpenApiTypes.STR) def get_type(self, instance): # It could be that the view related to the instance is already in the context # else we can call the specific_class property to find it. view = self.context.get('instance_type') if not view: view = view_type_registry.get_by_model(instance.specific_class) return view.type