Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modification for update or create reverse relations method in mixin #177

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions drf_writable_nested/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,18 @@ 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 = []
for data in related_data:
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,
Expand Down Expand Up @@ -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,
Expand Down