Categories
Python Answers

How to clone a Python Django model instance object and save it to the database?

Spread the love

To clone a Python Django model instance object and save it to the database, we can use the get method.

For instance, we write

obj = Foo.objects.get(pk=<some_existing_pk>)
obj.pk = None
obj.save()

to get the object with Foo.objects.get.

And then we set pk to None.

Finally, we call save to save the data.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *