From ed3a6b3a011a3b969ec61cf8a0f17323c84d1644 Mon Sep 17 00:00:00 2001 From: Constantin Slednev Date: Thu, 8 Jun 2023 14:56:42 +0300 Subject: [PATCH 1/3] pass additional context to the serializer --- drf_writable_nested/mixins.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drf_writable_nested/mixins.py b/drf_writable_nested/mixins.py index f40dd48..0bc605c 100644 --- a/drf_writable_nested/mixins.py +++ b/drf_writable_nested/mixins.py @@ -177,6 +177,9 @@ def update_or_create_reverse_relations(self, instance, reverse_relations): obj = instances.get( self._get_related_pk(data, field.Meta.model) ) + if not obj and (getattr(field, 'only_assignation_allowed', False) or + getattr(field.parent, 'only_assignation_allowed', False)): + continue serializer = self._get_serializer_for_field( field, instance=obj, @@ -212,6 +215,9 @@ def update_or_create_direct_relations(self, attrs, relations): obj = model_class.objects.filter( pk=pk, ).first() + else: + if field.only_assignation_allowed: + return serializer = self._get_serializer_for_field( field, instance=obj, From 1b2569252d4c748e43ceb8b42a8ae6b7ecf8c1ff Mon Sep 17 00:00:00 2001 From: Constantin Slednev Date: Thu, 8 Jun 2023 22:16:56 +0300 Subject: [PATCH 2/3] typo error --- drf_writable_nested/mixins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drf_writable_nested/mixins.py b/drf_writable_nested/mixins.py index 0bc605c..6985de3 100644 --- a/drf_writable_nested/mixins.py +++ b/drf_writable_nested/mixins.py @@ -216,7 +216,7 @@ def update_or_create_direct_relations(self, attrs, relations): pk=pk, ).first() else: - if field.only_assignation_allowed: + if getattr(field, 'only_assignation_allowed', False): return serializer = self._get_serializer_for_field( field, From 48f0ffe81e05bf9926a7e0d97215aead90e8d68c Mon Sep 17 00:00:00 2001 From: qzibet Date: Mon, 12 Jun 2023 11:42:54 +0600 Subject: [PATCH 3/3] add modification for many-to-many fields with though argument in `update_or_create_reverse_relations` method --- drf_writable_nested/mixins.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drf_writable_nested/mixins.py b/drf_writable_nested/mixins.py index 6985de3..4c6678f 100644 --- a/drf_writable_nested/mixins.py +++ b/drf_writable_nested/mixins.py @@ -170,6 +170,8 @@ def update_or_create_reverse_relations(self, instance, reverse_relations): ) elif not related_field.many_to_many: save_kwargs[related_field.name] = instance + elif related_field.many_to_many and related_field.remote_field.through: + save_kwargs['parent_instance'] = instance new_related_instances = [] errors = [] @@ -209,6 +211,7 @@ def update_or_create_direct_relations(self, attrs, relations): for field_name, (field, field_source) in relations.items(): obj = None data = self.get_initial()[field_name] + model_class = field.Meta.model pk = self._get_related_pk(data, model_class) if pk: @@ -217,7 +220,7 @@ def update_or_create_direct_relations(self, attrs, relations): ).first() else: if getattr(field, 'only_assignation_allowed', False): - return + continue serializer = self._get_serializer_for_field( field, instance=obj,