# Proposal: Add a method to create a copy of a model instance

**URL:** <https://forum.djangoproject.com/t/proposal-add-a-method-to-create-a-copy-of-a-model-instance/23896>\
**Category:** ORM\
**Created:** [September 16, 2023, 3:38am UTC](https://forum.djangoproject.com/t/proposal-add-a-method-to-create-a-copy-of-a-model-instance/23896 "2023-09-16T03:38:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hrushikeshrv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hrushikeshrv/32/27602_2.png) [@hrushikeshrv](https://forum.djangoproject.com/u/hrushikeshrv)\
**Post date:** [September 16, 2023, 3:38am UTC](https://forum.djangoproject.com/t/proposal-add-a-method-to-create-a-copy-of-a-model-instance/23896/1 "2023-09-16T03:38:08Z")

</div>

Currently the documented way to create a copy of a model instance is by setting its `pk` to `None` and saving the instance again - [see link](https://docs.djangoproject.com/en/4.2/topics/db/queries/#copying-model-instances). It also states that there is no built-in method for copying model instances. I’m not sure if such a method was not provided by design, but from my research through Trac and the forum, I couldn’t find a reason as to why it can’t/shouldn’t exist.

Would it be feasible to add such a method? We could then expose parameters to control exactly how the instance is copied (with or without FK or M2M relations, what should be done about `OneToOneField`s, and so on).

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [September 16, 2023, 8:21pm UTC](https://forum.djangoproject.com/t/proposal-add-a-method-to-create-a-copy-of-a-model-instance/23896/2 "2023-09-16T20:21:11Z")

</div>

Through `git blame` on the documentation section, I found [the related commit](https://github.com/django/django/commit/a97ecfdea80080da7e522d8dd8a4792a777694f9) and [ticket](https://code.djangoproject.com/ticket/4027). There’s a lengthy discussion on the ticket about the complexities with M2M fields etc.

I think at this point it would need some deep research and testing to present a possible solution. It may also best to iterate on a solution in a third party package.

---

<div class="post-metadata">

**Author:** ![hrushikeshrv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hrushikeshrv/32/27602_2.png) [@hrushikeshrv](https://forum.djangoproject.com/u/hrushikeshrv)\
**Post date:** [September 19, 2023, 6:03am UTC](https://forum.djangoproject.com/t/proposal-add-a-method-to-create-a-copy-of-a-model-instance/23896/3 "2023-09-19T06:03:59Z")

</div>

I agree that there are complexities with M2M and related fields, but if it would be okay to have a simpler implementation (possibly without support for deep copying and leaving out the case of inherited models, etc.), I’d like to take a stab at implementing this.

Just wanted to make sure that the idea wasn’t too outrageous!

---

<div class="post-metadata">

**Author:** ![shangxiao](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shangxiao/32/10313_2.png) [@shangxiao](https://forum.djangoproject.com/u/shangxiao)\
**Post date:** [September 19, 2023, 6:20am UTC](https://forum.djangoproject.com/t/proposal-add-a-method-to-create-a-copy-of-a-model-instance/23896/4 "2023-09-19T06:20:10Z")

</div>

Like Adam touched on, often a good idea is to try out implementing it as a 3rd party package and share it with people. If it’s something that becomes quite popular and takes off then it could be considered a candidate for inclusion into the core framework.

In this particular case there’s no reason why it couldn’t start out as a separate function instead of an instance method.

---

<div class="post-metadata">

**Author:** ![shangxiao](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shangxiao/32/10313_2.png) [@shangxiao](https://forum.djangoproject.com/u/shangxiao)\
**Post date:** [September 19, 2023, 7:14am UTC](https://forum.djangoproject.com/t/proposal-add-a-method-to-create-a-copy-of-a-model-instance/23896/5 "2023-09-19T07:14:05Z")

</div>

Also just FYI another good option is to propose it for inclusion into django-exensions: they have a great selection of niche extensions for Django. It might be a great way to gauge it’s usefulness for inclusion into core like `shell_plus` which has been considered as a nice prototype for improving core’s `shell`.

---

<div class="post-metadata">

**Author:** ![hrushikeshrv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hrushikeshrv/32/27602_2.png) [@hrushikeshrv](https://forum.djangoproject.com/u/hrushikeshrv)\
**Post date:** [September 19, 2023, 8:15am UTC](https://forum.djangoproject.com/t/proposal-add-a-method-to-create-a-copy-of-a-model-instance/23896/6 "2023-09-19T08:15:59Z")

</div>

> Also just FYI another good option is to propose it for inclusion into django-extensions.

That seems like a better fit for this feature. I’ll do that. Thanks!
