def test_non_api_url_is_properly_reversed_regardless_of_the_version(self):
        """
        Regression test for #2711
        """
        field = self._create_field('non-api-view', 'v1')
        assert field.to_representation(PKOnlyObject(10)) == 'http://testserver/non-api/10/'

        field = self._create_field('non-api-view', 'v2')
        assert field.to_representation(PKOnlyObject(10)) == 'http://testserver/non-api/10/'
示例#2
0
    def get_attribute(self, instance):
        if self.use_pk_only_optimization() and self.source_attrs:
            # Optimized case, return a mock object only containing the pk attribute.
            try:
                instance = get_attribute(instance, self.source_attrs[:-1])
                value = instance.serializable_value(self.source_attrs[-1])
                if is_simple_callable(value):
                    # Handle edge case where the relationship `source` argument
                    # points to a `get_relationship()` method on the model
                    value = value().pk
                return PKOnlyObject(pk=value)
            except AttributeError:
                pass

        # Standard case, return the object instance.
        return get_attribute(instance, self.source_attrs)
 def test_api_url_is_properly_reversed_with_nested(self):
     field = self._create_field('nested', 'v1:nested-namespace')
     assert field.to_representation(
         PKOnlyObject(3)) == 'http://testserver/v1/nested/namespaced/3/'
 def test_api_url_is_properly_reversed_with_v2(self):
     field = self._create_field('namespaced', 'v2')
     assert field.to_representation(
         PKOnlyObject(5)) == 'http://testserver/v2/namespaced/5/'
示例#5
0
 def get_attribute(self, instance):
     """Return the entire object versus just the attribute value."""
     self.queryset = type(instance.history_object)._default_manager
     return PKOnlyObject(
         pk=instance.serializable_value('history_object').pk)