コード例 #1
0
    def _create_field(self, view_name, version):
        request = factory.get("/")
        request.versioning_scheme = NamespaceVersioning()
        request.version = version

        field = serializers.HyperlinkedRelatedField(view_name=view_name,
                                                    read_only=True)
        field._context = {'request': request}
        return field
コード例 #2
0
    def setUp(self):
        super(TestHyperlinkedRelatedField, self).setUp()

        class MockQueryset(object):
            def get(self, pk):
                return 'object %s' % pk

        self.field = serializers.HyperlinkedRelatedField(
            view_name='namespaced', queryset=MockQueryset())
        request = factory.get('/')
        request.versioning_scheme = NamespaceVersioning()
        request.version = 'v1'
        self.field._context = {'request': request}
コード例 #3
0
 def setUp(self):
     self.queryset = MockQueryset([
         MockObject(pk=1, name='foobar'),
         MockObject(pk=2, name='bazABCqux'),
     ])
     self.field = serializers.HyperlinkedRelatedField(
         view_name='example',
         lookup_field='name',
         lookup_url_kwarg='name',
         queryset=self.queryset,
     )
     self.field.reverse = mock_reverse
     self.field._context = {'request': True}
コード例 #4
0
    def hyperlinked_related_queryset_error(self, exc_type):
        class QuerySet:
            def get(self, *args, **kwargs):
                raise exc_type

        field = serializers.HyperlinkedRelatedField(
            view_name='example',
            lookup_field='name',
            queryset=QuerySet(),
        )
        with pytest.raises(serializers.ValidationError) as excinfo:
            field.to_internal_value('http://example.org/example/doesnotexist/')
        msg = excinfo.value.detail[0]
        assert msg == 'Invalid hyperlink - Object does not exist.'
コード例 #5
0
 class TestSerializer(serializers.Serializer):
     via_unreachable = serializers.HyperlinkedRelatedField(
         source='does_not_exist.unreachable',
         view_name='example',
         read_only=True,
     )