2. How to copy or clone an existing model object?¶
There is no built-in method for copying model instances, it is possible to create new instance with all fields values copied.
If an instance is saved with instance’s
pk set to
None, the instance is used to create a new record in the DB. That means every field other than the
PK is copied.
In : Hero.objects.all().count() Out: 4 In : hero = Hero.objects.first() In : hero.pk = None In : hero.save() In : Hero.objects.all().count() Out: 5