Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion model_clone/mixins/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,10 @@ def _create_copy_of_instance(instance, force=False, sub_clone=False):
]
):
# Do not try to get unique value for enum type field
if isinstance(f, models.CharField) and not f.choices:
if (
isinstance(f, (models.CharField, models.TextField))
and not f.choices
):
value = clean_value(value, unique_duplicate_suffix)
if use_unique_duplicate_suffix:
value = get_unique_value(
Expand Down
9 changes: 9 additions & 0 deletions model_clone/tests/test_clone_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
BookSaleTag,
Tag,
SaleTag,
Product,
)

User = get_user_model()
Expand Down Expand Up @@ -649,3 +650,11 @@ def test_cloning_multiple_instances_with_autocommit_is_valid(self):
clone.first_name,
r"{}\s[\d]".format(Author.UNIQUE_DUPLICATE_SUFFIX),
)

def test_cloning_model_with_unique_text_field(self):
product = Product.objects.create(name="Test Product")
clone = product.make_clone()
self.assertEqual(
clone.name,
"{0} {1} 1".format(product.name, Product.UNIQUE_DUPLICATE_SUFFIX),
)
2 changes: 1 addition & 1 deletion model_clone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def get_value(value, suffix, transform, max_length, index):
duplicate_suffix = " {} {}".format(suffix, index)
total_length = len(value + duplicate_suffix)

if total_length > max_length:
if max_length is not None and total_length > max_length:
# Truncate the value to max_length - suffix length.
value = value[: max_length - len(duplicate_suffix)]

Expand Down
28 changes: 28 additions & 0 deletions sample/migrations/0016_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.2.3 on 2021-05-17 12:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("sample", "0015_auto_20210423_0935"),
]

operations = [
migrations.CreateModel(
name="Product",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.TextField(unique=True)),
],
),
]
4 changes: 4 additions & 0 deletions sample/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,7 @@ class Cover(CloneModel):
class BackCover(models.Model):
content = models.CharField(max_length=200)
book = models.OneToOneField(Book, on_delete=models.CASCADE)


class Product(CloneMixin, models.Model):
name = models.TextField(unique=True)