diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py index a3dbe8b061caab1615c01556e31872f3a9b4321c..291ae714efebd01e0d26976d693e9de7dea2b282 100644 --- a/djangoldp/serializers.py +++ b/djangoldp/serializers.py @@ -60,7 +60,7 @@ class LDListMixin: object_list)) else: container_id = Model.container_id(self.parent.instance) - obj = next(filter(lambda o: container_id in o[self.parent.url_field_name], object_list)) + obj = next(filter(lambda o: container_id.lstrip('/') in o[self.parent.url_field_name], object_list)) list = super().get_value(obj) try: list = next( @@ -256,7 +256,7 @@ class LDPSerializer(HyperlinkedModelSerializer): return super().get_value(obj) else: resource_id = Model.resource_id(self.parent.instance) - obj = next(filter(lambda o: resource_id in o[self.parent.url_field_name], object_list)) + obj = next(filter(lambda o: resource_id.lstrip('/') in o[self.parent.url_field_name], object_list)) return super().get_value(obj) except KeyError: return super().get_value(dictionary) diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py index aac98626b90379690b925b5edcd52ddbb303c218..468377c8d7f2c07cce69f468c039967b423e14b4 100644 --- a/djangoldp/tests/models.py +++ b/djangoldp/tests/models.py @@ -1,4 +1,5 @@ from django.conf import settings +from django.contrib.auth import get_user_model from django.db import models from djangoldp.models import Model @@ -78,3 +79,6 @@ class Post(Model): class Meta: auto_author = 'author' + + +get_user_model()._meta.serializer_fields = ['@id', 'username', 'first_name', 'last_name', 'email', 'conversation_set'] diff --git a/djangoldp/tests/tests_update.py b/djangoldp/tests/tests_update.py index c94facfe799e8b0d87cda7b30dceb6e878ae4538..41f10a9089c5b2f3f92d0578f3b785d12873194a 100644 --- a/djangoldp/tests/tests_update.py +++ b/djangoldp/tests/tests_update.py @@ -266,7 +266,28 @@ class Update(TestCase): body = [{ '@id': '/posts/{}/'.format(post.pk), 'http://happy-dev.fr/owl/#content': "post content"}] - response = self.client.put('/posts/{}/'.format(post.pk), data=json.dumps(body), content_type='application/ld+json') + response = self.client.put('/posts/{}/'.format(post.pk), data=json.dumps(body), + content_type='application/ld+json') self.assertEqual(response.status_code, 200) self.assertEquals(response.data['content'], "post content") self.assertIn('location', response._headers) + + def test_create_sub_object_in_existing_object_with_reverse_relation(self): + user = User.objects.create(username="alex", password="test") + body = [ + { + '@id': "_:b975", + 'http://happy-dev.fr/owl/#description': "conversation description" + }, + { + '@id': '/users/{}/'.format(user.pk), + "http://happy-dev.fr/owl/#first_name": "Alexandre", + "http://happy-dev.fr/owl/#last_name": "Bourlier", + "http://happy-dev.fr/owl/#username": "alex", + 'http://happy-dev.fr/owl/#conversation_set': {'@id': "_:b975"} + } + ] + response = self.client.put('/users/{}/'.format(user.pk), data=json.dumps(body), + content_type='application/ld+json') + self.assertEqual(response.status_code, 200) + self.assertIn('conversation_set', response.data)