Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Post(models.Model):
- text = models.CharField(max_length=256)
- type = models.ForeignKey(PostType)
- class PostSerializer(serializers.HyperlinkedModelSerializer):
- type = serializers.SlugRelatedField(queryset=PostType.objects.all(), slug_field='id', allow_null=True, required=False)
- class PostTests(APITestCase):
- def test_create(self):
- response = self.client.post('/api/post/', {'text': 'Hello world !'})
- # Expected
- 200
- # Actual behavior
- File "/usr/local/lib/python3.5/site-packages/rest_framework/serializers.py", line 509, in data
- ret = super(Serializer, self).data
- File "/usr/local/lib/python3.5/site-packages/rest_framework/serializers.py", line 241, in data
- self._data = self.to_representation(self.validated_data)
- File "/usr/local/lib/python3.5/site-packages/rest_framework/serializers.py", line 469, in to_representation
- attribute = field.get_attribute(instance)
- File "/usr/local/lib/python3.5/site-packages/rest_framework/relations.py", line 165, in get_attribute
- return get_attribute(instance, self.source_attrs)
- File "/usr/local/lib/python3.5/site-packages/rest_framework/fields.py", line 79, in get_attribute
- instance = instance[attr]
- KeyError: 'type'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement