diff --git a/drf_writable_nested/mixins.py b/drf_writable_nested/mixins.py index f40dd48..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 = [] @@ -177,6 +179,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, @@ -206,12 +211,16 @@ 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: obj = model_class.objects.filter( pk=pk, ).first() + else: + if getattr(field, 'only_assignation_allowed', False): + continue serializer = self._get_serializer_for_field( field, instance=obj,