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

Fix invalid enum field value for unique_together fields #178

Merged
merged 4 commits into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
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: 6 additions & 3 deletions model_clone/mixins/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def _create_copy_of_instance(cls, instance):
]
):
value = getattr(instance, f.attname, f.get_default())
if f.attname in unique_fields and isinstance(f, models.CharField):
# Do not try to get unique value for enum type field
if f.attname in unique_fields and isinstance(f, models.CharField) and not f.choices:
value = clean_value(value, cls.UNIQUE_DUPLICATE_SUFFIX)
if cls.USE_UNIQUE_DUPLICATE_SUFFIX:
value = get_unique_value(
Expand Down Expand Up @@ -331,9 +332,11 @@ def make_clone(self, attrs=None, sub_clone=False):
items = []
for item in getattr(self, field.related_name).all():
try:
item_clone = item.make_clone()
item_clone = item.make_clone(attrs={field.remote_field.name: duplicate})
except IntegrityError:
item_clone = item.make_clone(sub_clone=True)
item_clone = item.make_clone(
attrs={field.remote_field.name: duplicate}, sub_clone=True
)
items.append(item_clone)

getattr(duplicate, field.related_name).set(items)
Expand Down
23 changes: 22 additions & 1 deletion model_clone/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_cloning_unique_field_with_use_unique_duplicate_suffix_set_to_False(
with self.assertRaises(IntegrityError):
author.make_clone()

use_unique_duplicate_suffix_mock.assert_called_once()
use_unique_duplicate_suffix_mock.assert_called()

@patch("sample.models.Author.UNIQUE_DUPLICATE_SUFFIX", new_callable=PropertyMock)
def test_cloning_unique_field_with_a_custom_unique_duplicate_suffix(
Expand All @@ -147,6 +147,27 @@ def test_cloning_unique_field_with_a_custom_unique_duplicate_suffix(
author_clone.first_name, "{} {} {}".format(first_name, "new", 1),
)

def test_cloning_unique_together_fields_with_enum_field(self):
first_name = "Ruby"
last_name = "Jack"

author = Author.objects.create(
first_name=first_name, last_name=last_name, age=26, sex="F", created_by=self.user
)

author_clone = author.make_clone()

self.assertNotEqual(author.pk, author_clone.pk)
self.assertEqual(author.sex, author_clone.sex)
self.assertEqual(
author_clone.first_name,
"{} {} {}".format(first_name, Author.UNIQUE_DUPLICATE_SUFFIX, 1),
)
self.assertEqual(
author_clone.last_name,
"{} {} {}".format(last_name, Author.UNIQUE_DUPLICATE_SUFFIX, 1),
)

def test_cloning_unique_fields_max_length(self):
"""Max unique field length handling

Expand Down
17 changes: 17 additions & 0 deletions sample/migrations/0007_auto_20200829_0213.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.0.4 on 2020-08-29 02:13

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('sample', '0006_assignment'),
]

operations = [
migrations.AlterUniqueTogether(
name='author',
unique_together={('first_name', 'last_name', 'sex')},
),
]
3 changes: 3 additions & 0 deletions sample/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def __str__(self):
def full_name(self):
return "{} {}".format(self.first_name, self.last_name)

class Meta:
unique_together = (("first_name", "last_name", "sex"),)


class Book(CloneModel):
name = models.CharField(max_length=2000)
Expand Down