django-clone icon indicating copy to clipboard operation
django-clone copied to clipboard

[BUG] Fail to clone an object with generatedField

Open anotine10 opened this issue 5 months ago • 1 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Does this issue exist in the latest version?

  • [x] I'm using the latest release

Describe the bug?

it raises an exception when i try to clone an object which has one GeneratedField

To Reproduce

I have some object with some generated field. with the following script,

cloned = my_obj.make_clone(
        attrs={
            "phone": phone_number_formatted,
        
        }
    )

it fails:

...
web-1           |   File "/usr/local/lib/python3.13/contextlib.py", line 85, in inner
web-1           |     return func(*args, **kwds)
web-1           |   File "/usr/local/lib/python3.13/site-packages/model_clone/mixin.py", line 277, in make_clone
web-1           |     duplicate = self._create_copy_of_instance(self, using=using, parent=parent)
web-1           |   File "/usr/local/lib/python3.13/site-packages/model_clone/mixin.py", line 396, in _create_copy_of_instance
web-1           |     f.pre_save(new_instance, add=True)
web-1           |     ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
web-1           |   File "/usr/local/lib/python3.13/site-packages/django/db/models/fields/__init__.py", line 990, in pre_save
web-1           |     return getattr(model_instance, self.attname)
web-1           |   File "/usr/local/lib/python3.13/site-packages/django/db/models/query_utils.py", line 224, in __get__
web-1           |     raise AttributeError(
web-1           |         "Cannot read a generated field from an unsaved model."
web-1           |     )

What OS are you seeing the problem on?

macOS

Expected behavior?

it should filter out the generatedfield

Relevant log output


Anything else?

No response

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

anotine10 avatar Jul 14 '25 16:07 anotine10