diff --git a/model_clone/mixins/clone.py b/model_clone/mixins/clone.py index 8ab09d13..d10306a7 100644 --- a/model_clone/mixins/clone.py +++ b/model_clone/mixins/clone.py @@ -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( diff --git a/model_clone/tests/test_clone_mixin.py b/model_clone/tests/test_clone_mixin.py index 4ce69ef1..c6bf3d51 100644 --- a/model_clone/tests/test_clone_mixin.py +++ b/model_clone/tests/test_clone_mixin.py @@ -25,6 +25,7 @@ BookSaleTag, Tag, SaleTag, + Product, ) User = get_user_model() @@ -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), + ) diff --git a/model_clone/utils.py b/model_clone/utils.py index 0fad5476..64a1a855 100644 --- a/model_clone/utils.py +++ b/model_clone/utils.py @@ -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)] diff --git a/sample/migrations/0016_product.py b/sample/migrations/0016_product.py new file mode 100644 index 00000000..8d069263 --- /dev/null +++ b/sample/migrations/0016_product.py @@ -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)), + ], + ), + ] diff --git a/sample/models.py b/sample/models.py index 0e0b88e8..ebcdd3ae 100644 --- a/sample/models.py +++ b/sample/models.py @@ -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)