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

Use default value only for special fields like timestamp, uuid #399

Merged
merged 3 commits into from
Jun 29, 2021
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: 4 additions & 5 deletions model_clone/mixins/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,11 @@ def _create_copy_of_instance(instance, using=None, force=False, sub_clone=False)
new_instance = cls()

for f in fields:
value = f.get_default() or getattr(instance, f.attname, None)
if not f.editable:
f.pre_save(new_instance, add=True)
continue

if isinstance(f, (models.DateTimeField, models.DateField)):
if f.auto_now or f.auto_now_add:
f.pre_save(new_instance, add=True)
continue
value = getattr(instance, f.attname)

if all(
[
Expand Down
22 changes: 21 additions & 1 deletion model_clone/tests/test_clone_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ def test_cloning_model_with_custom_id(self):
clone = instance.make_clone({"user": self.user2})
self.assertNotEqual(instance.pk, clone.pk)

@patch("sample.models.Book._clone_excluded_fields", new_callable=PropertyMock)
@patch("sample.models.Library._clone_excluded_fields", new_callable=PropertyMock)
jackton1 marked this conversation as resolved.
Show resolved Hide resolved
def test_cloning_model_with_excluded_fields(self, _clone_excluded_fields_mock):
_clone_excluded_fields_mock.return_value = ["name"]
instance = Library.objects.create(name="First library", user=self.user1)
clone = instance.make_clone({"name": "New Library"})
self.assertNotEqual(instance.pk, clone.pk)
self.assertNotEqual(instance.name, clone.name)

def test_cloning_explict_fields(self):
name = "New Library"
Expand All @@ -78,6 +79,25 @@ def test_cloning_explict_fields(self):
self.assertNotEqual(instance.pk, clone.pk)
self.assertNotEqual(instance.name, clone.name)

@patch("sample.models.Book._clone_fields", new_callable=PropertyMock)
def test_cloning_with_default_value(self, _clone_fields_mock):
instance = Book.objects.create(
name="Not Published Book", created_by=self.user1, published_at=None
)
_clone_fields_mock.return_value = ["name", "created_by", "slug", "published_at"]
clone = instance.make_clone()
self.assertNotEqual(instance.pk, clone.pk)
self.assertEqual(instance.name, clone.name)
self.assertNotEqual(instance.slug, clone.slug)
self.assertIsNone(clone.published_at)

_clone_fields_mock.return_value.remove("published_at")
clone = instance.make_clone()
self.assertNotEqual(instance.pk, clone.pk)
self.assertEqual(instance.name, clone.name)
self.assertNotEqual(instance.slug, clone.slug)
self.assertIsNotNone(clone.published_at)

def test_cloning_unique_fk_field(self):
name = "New Library"
instance = Library.objects.create(name=name, user=self.user1)
Expand Down
33 changes: 33 additions & 0 deletions sample/migrations/0018_auto_20210628_2301.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.1.8 on 2021-06-28 23:01

from django.db import migrations, models
from django.utils import timezone


class Migration(migrations.Migration):

dependencies = [
("sample", "0017_auto_20210624_1117"),
]

operations = [
migrations.AlterField(
model_name="author",
name="last_name",
field=models.CharField(blank=True, default="Unknown", max_length=200),
),
migrations.AlterField(
model_name="author",
name="sex",
field=models.CharField(
choices=[("U", "Unknown"), ("F", "Female"), ("M", "Male")],
default="U",
max_length=1,
),
),
migrations.AddField(
model_name="book",
name="published_at",
field=models.DateTimeField(blank=True, default=timezone.now, null=True),
),
]
6 changes: 4 additions & 2 deletions sample/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@

class Author(CloneModel):
first_name = models.CharField(max_length=200, blank=True, unique=True)
last_name = models.CharField(max_length=200, blank=True)
last_name = models.CharField(default="Unknown", max_length=200, blank=True)
age = models.PositiveIntegerField()

uuid = models.UUIDField(default=uuid4, unique=True, editable=False)

SEX_CHOICES = [
("U", "Unknown"),
("F", "Female"),
("M", "Male"),
]
sex = models.CharField(choices=SEX_CHOICES, max_length=1)
sex = models.CharField(choices=SEX_CHOICES, max_length=1, default="U")
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
Expand Down Expand Up @@ -62,6 +63,7 @@ class Book(CloneModel):
created_at = models.DateTimeField(auto_now_add=True)
tags = models.ManyToManyField(Tag, through="BookTag")
sale_tags = models.ManyToManyField(SaleTag, through="BookSaleTag")
published_at = models.DateTimeField(null=True, blank=True, default=timezone.now)

def __str__(self):
return _(self.name)
Expand Down