Skip to content

Commit

Permalink
Merge branch 'int-ua-develop' into develop
Browse files Browse the repository at this point in the history
* int-ua-develop:
  Fixing ManyToMany merging with intermediary models
  Fixing ManyToMany merging with intermediary models
  Fixing ManyToMany merging, #108
  • Loading branch information
saxix committed Mar 19, 2017
2 parents cff9a66 + b0ff3ca commit 1c37cd3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/adminactions/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.fields import FieldDoesNotExist
from django.db.models.fields.related import ManyToManyField, OneToOneField
from django.db.models.fields.related import ManyToManyField, ManyToManyRel,\
OneToOneField
from django.http import HttpResponse
from django.utils import dateformat
from django.utils.encoding import force_text, smart_str, smart_text
Expand Down Expand Up @@ -81,7 +82,9 @@ def merge(master, other, fields=None, commit=False, m2m=None, related=None): #
# for rel in master._meta.get_all_related_objects(False, False, False)]

if m2m == ALL_FIELDS:
m2m = [field.name for field in master._meta.many_to_many]
m2m = [field.name
for field in master._meta.get_fields()
if field.many_to_many]

if m2m and not commit:
raise ValueError('Cannot save related with `commit=False`')
Expand All @@ -97,8 +100,11 @@ def merge(master, other, fields=None, commit=False, m2m=None, related=None): #
for fieldname in set(m2m):
all_m2m[fieldname] = []
field_object = get_field_by_path(master, fieldname)
if not isinstance(field_object, ManyToManyField):
if not isinstance(field_object, (ManyToManyField, ManyToManyRel)):
raise ValueError('{0} is not a ManyToManyField field'.format(fieldname))
if isinstance(field_object, ManyToManyField) \
and not field_object.rel.through._meta.auto_created:
continue
source_m2m = getattr(other, field_object.name)
for r in source_m2m.all():
all_m2m[fieldname].append(r)
Expand Down

0 comments on commit 1c37cd3

Please sign in to comment.