ensure no self-merges
This commit is contained in:
parent
65f2ae7dac
commit
8e603c6329
@ -3,6 +3,9 @@ def merge(from_obj, to_obj):
|
|||||||
if not isinstance(from_obj, type(to_obj)):
|
if not isinstance(from_obj, type(to_obj)):
|
||||||
raise ValueError("both objects must be of the same type")
|
raise ValueError("both objects must be of the same type")
|
||||||
|
|
||||||
|
if from_obj.pk == to_obj.pk:
|
||||||
|
raise ValueError("cannot merge object with itself")
|
||||||
|
|
||||||
for related in from_obj._meta.get_all_related_objects():
|
for related in from_obj._meta.get_all_related_objects():
|
||||||
accessor_name = related.get_accessor_name()
|
accessor_name = related.get_accessor_name()
|
||||||
varname = related.field.name
|
varname = related.field.name
|
||||||
|
@ -12,6 +12,11 @@ class MergeTests(TestCase):
|
|||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
merge(self.a, self.g)
|
merge(self.a, self.g)
|
||||||
|
|
||||||
|
def test_same_object(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
# make sure this doesn't rely on is
|
||||||
|
merge(self.a, Person.objects.get(name=self.a.name))
|
||||||
|
|
||||||
def test_fk_simple(self):
|
def test_fk_simple(self):
|
||||||
Number.objects.create(person=self.a, number='555-1111')
|
Number.objects.create(person=self.a, number='555-1111')
|
||||||
Number.objects.create(person=self.a, number='555-1112')
|
Number.objects.create(person=self.a, number='555-1112')
|
||||||
|
Loading…
Reference in New Issue
Block a user