fixes for Django 1.7
This commit is contained in:
parent
bd01e38f35
commit
2c69a6dda1
@ -1,4 +1,5 @@
|
|||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
class MergeException(Exception):
|
class MergeException(Exception):
|
||||||
@ -25,10 +26,8 @@ def merge(from_obj, to_obj, one_to_one_conflict=ERROR):
|
|||||||
accessor_name = related.get_accessor_name()
|
accessor_name = related.get_accessor_name()
|
||||||
varname = related.field.name
|
varname = related.field.name
|
||||||
|
|
||||||
if related.multiple:
|
# in Django 1.8 can test for related.one_to_one
|
||||||
field = getattr(from_obj, accessor_name)
|
if isinstance(related.field, models.OneToOneField):
|
||||||
field.all().update(**{varname: to_obj})
|
|
||||||
elif related.one_to_one:
|
|
||||||
try:
|
try:
|
||||||
field = getattr(from_obj, accessor_name)
|
field = getattr(from_obj, accessor_name)
|
||||||
try:
|
try:
|
||||||
@ -36,14 +35,12 @@ def merge(from_obj, to_obj, one_to_one_conflict=ERROR):
|
|||||||
if one_to_one_conflict == KEEP:
|
if one_to_one_conflict == KEEP:
|
||||||
pass # do nothing
|
pass # do nothing
|
||||||
elif one_to_one_conflict == DELETE:
|
elif one_to_one_conflict == DELETE:
|
||||||
# if null:
|
|
||||||
# setattr(to_field, varname, None)
|
|
||||||
# to_field.save()
|
|
||||||
to_field.delete()
|
to_field.delete()
|
||||||
setattr(field, varname, to_obj)
|
setattr(field, varname, to_obj)
|
||||||
field.save()
|
field.save()
|
||||||
else:
|
else:
|
||||||
raise OneToOneConflict("both fields have an attribute set for {}".format(accessor_name))
|
raise OneToOneConflict(
|
||||||
|
"both fields have an attribute set for {}".format(accessor_name))
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
# doesn't exist, safe to overwrite
|
# doesn't exist, safe to overwrite
|
||||||
setattr(field, varname, to_obj)
|
setattr(field, varname, to_obj)
|
||||||
@ -51,6 +48,10 @@ def merge(from_obj, to_obj, one_to_one_conflict=ERROR):
|
|||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
# from_obj one to one isn't set, skip
|
# from_obj one to one isn't set, skip
|
||||||
pass
|
pass
|
||||||
|
# in Django 1.8 can test for related.multiple
|
||||||
|
elif isinstance(related.field, models.ForeignKey):
|
||||||
|
field = getattr(from_obj, accessor_name)
|
||||||
|
field.all().update(**{varname: to_obj})
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError('unexpected relation type, please file a bug')
|
raise NotImplementedError('unexpected relation type, please file a bug')
|
||||||
|
|
||||||
@ -63,7 +64,6 @@ def merge(from_obj, to_obj, one_to_one_conflict=ERROR):
|
|||||||
accessor_name = varname
|
accessor_name = varname
|
||||||
|
|
||||||
field = getattr(from_obj, accessor_name)
|
field = getattr(from_obj, accessor_name)
|
||||||
if related.many_to_many:
|
|
||||||
for f in field.all():
|
for f in field.all():
|
||||||
getattr(f, varname).remove(from_obj)
|
getattr(f, varname).remove(from_obj)
|
||||||
getattr(f, varname).add(to_obj)
|
getattr(f, varname).add(to_obj)
|
||||||
|
Loading…
Reference in New Issue
Block a user