Esempio n. 1
0
    class BasicNodeSerializer(JSONAPISerializer):

        parent = RelationshipField(related_view='nodes:node-detail',
                                   related_view_kwargs={'node_id': '<_id>'})

        parent_with_meta = RelationshipField(
            related_view='nodes:node-detail',
            related_view_kwargs={'node_id': '<_id>'},
            related_meta={
                'count': 'get_count',
                'extra': 'get_extra'
            },
        )

        self_and_related_field = RelationshipField(
            related_view='nodes:node-detail',
            related_view_kwargs={'node_id': '<_id>'},
            self_view='nodes:node-contributors',
            self_view_kwargs={'node_id': '<_id>'},
        )

        two_url_kwargs = RelationshipField(
            # fake url, for testing purposes
            related_view='nodes:node-pointer-detail',
            related_view_kwargs={
                'node_id': '<_id>',
                'node_link_id': '<_id>'
            },
        )

        # If related_view_kwargs is a callable, this field _must_ match the property name on
        # the target record
        registered_from = RelationshipField(
            related_view=lambda n: 'registrations:registration-detail'
            if n and n.is_registration else 'nodes:node-detail',
            related_view_kwargs=lambda n: {'node_id': '<registered_from._id>'})

        field_with_filters = base_serializers.RelationshipField(
            related_view='nodes:node-detail',
            related_view_kwargs={'node_id': '<_id>'},
            filter={
                'target': 'hello',
                'woop': 'yea'
            })

        class Meta:
            type_ = 'nodes'

        def get_count(self, obj):
            return 1

        def get_extra(self, obj):
            return 'foo'
Esempio n. 2
0
    class BasicNodeSerializer(JSONAPISerializer):

        parent = RelationshipField(related_view='nodes:node-detail',
                                   related_view_kwargs={'node_id': '<pk>'})

        parent_with_meta = RelationshipField(
            related_view='nodes:node-detail',
            related_view_kwargs={'node_id': '<pk>'},
            related_meta={
                'count': 'get_count',
                'extra': 'get_extra'
            },
        )

        self_and_related_field = RelationshipField(
            related_view='nodes:node-detail',
            related_view_kwargs={'node_id': '<pk>'},
            self_view='nodes:node-contributors',
            self_view_kwargs={'node_id': '<pk>'},
        )

        two_url_kwargs = RelationshipField(
            # fake url, for testing purposes
            related_view='nodes:node-pointer-detail',
            related_view_kwargs={
                'node_id': '<pk>',
                'node_link_id': '<pk>'
            },
        )

        not_attribute_on_target = RelationshipField(
            # fake url, for testing purposes
            related_view='nodes:node-children',
            related_view_kwargs={'node_id': '12345'})

        class Meta:
            type_ = 'nodes'

        def get_count(self, obj):
            return 1

        def get_extra(self, obj):
            return 'foo'