Пример #1
0
    def to_representation(self, instance):
        """
        Provides post processing. Sub-classes should implement their own
        to_representation method, but pass the resulting dict through
        this function to get tagging and field selection.

        Arguments:
            instance: Serialized dict, or object. If object,
                it will be serialized by the super class's
                to_representation() method.
        """

        if not isinstance(instance, dict):
            data = super(
                DynamicEphemeralSerializer,
                self
            ).to_representation(instance)
        else:
            data = instance
            instance = EphemeralObject(data)

        if self.id_only():
            return data
        else:
            return tag_dict(data, serializer=self, instance=instance)
Пример #2
0
    def to_representation(self, instance):
        """Modified to_representation method.

        Arguments:
            instance: A model instance or data object.
        Returns:
            Instance ID if the serializer is meant to represent its ID.
            Otherwise, a tagged data dict representation.
        """
        if self.id_only():
            return instance.pk
        else:
            if self.enable_optimization:
                representation = self._faster_to_representation(instance)
            else:
                representation = super(
                    WithDynamicSerializerMixin,
                    self
                ).to_representation(instance)

            if settings.ENABLE_LINKS:
                # TODO: Make this function configurable to support other
                #       formats like JSON API link objects.
                representation = merge_link_object(
                    self, representation, instance
                )

        # tag the representation with the serializer and instance
        return tag_dict(
            representation,
            serializer=self,
            instance=instance,
            embed=self.embed
        )
Пример #3
0
    def _to_representation(self, instance):
        """Uncached `to_representation`."""

        if self.enable_optimization:
            representation = self._faster_to_representation(instance)
        else:
            representation = super(WithDynamicSerializerMixin,
                                   self).to_representation(instance)

        if settings.ENABLE_LINKS:
            # TODO: Make this function configurable to support other
            #       formats like JSON API link objects.
            representation = merge_link_object(self, representation, instance)

        if self.debug:
            representation['_meta'] = {
                'id': instance.pk,
                'type': self.get_plural_name()
            }

        # tag the representation with the serializer and instance
        return tag_dict(representation,
                        serializer=self,
                        instance=instance,
                        embed=self.embed)
Пример #4
0
    def to_representation(self, instance):
        """
        Provides post processing. Sub-classes should implement their own
        to_representation method, but pass the resulting dict through
        this function to get tagging and field selection.

        Arguments:
            instance: Serialized dict, or object. If object,
                it will be serialized by the super class's
                to_representation() method.
        """

        if not isinstance(instance, dict):
            data = super(
                DynamicEphemeralSerializer,
                self
            ).to_representation(instance)
        else:
            data = instance
            instance = EphemeralObject(data)

        if self.id_only():
            return data
        else:
            return tag_dict(data, serializer=self, instance=instance)
Пример #5
0
    def to_representation(self, instance):
        """Modified to_representation method.

        Arguments:
            instance: A model instance or data object.
        Returns:
            Instance ID if the serializer is meant to represent its ID.
            Otherwise, a tagged data dict representation.
        """
        if self.id_only():
            return instance.pk
        else:
            if self.enable_optimization:
                representation = self._faster_to_representation(instance)
            else:
                representation = super(
                    WithDynamicSerializerMixin,
                    self
                ).to_representation(instance)

            if settings.ENABLE_LINKS:
                # TODO: Make this function configurable to support other
                #       formats like JSON API link objects.
                representation = merge_link_object(
                    self, representation, instance
                )

        # tag the representation with the serializer and instance
        return tag_dict(
            representation,
            serializer=self,
            instance=instance,
            embed=self.embed
        )
Пример #6
0
    def to_representation(self, instance):
        """Modified to_representation method.

        Arguments:
            instance: A model instance or data object.
        Returns:
            Instance ID if the serializer is meant to represent its ID.
            Otherwise, a tagged data dict representation.
        """
        id_only = self.id_only()
        if (self.get_format() == 'admin' and self.is_root()):
            id_only = False
        if id_only:
            return instance.pk
        else:
            if self.enable_optimization:
                representation = self._faster_to_representation(instance)
            else:
                representation = super(WithDynamicSerializerMixin,
                                       self).to_representation(instance)

            query_params = self.get_request_attribute('query_params', {})
            if (settings.ENABLE_LINKS and 'exclude_links' not in query_params):
                representation = merge_link_object(self, representation,
                                                   instance)

        if self.debug:
            representation['_meta'] = {
                'id': instance.pk,
                'type': self.get_plural_name()
            }

        # tag the representation with the serializer and instance
        return tag_dict(representation,
                        serializer=self,
                        instance=instance,
                        embed=self.embed)