Regis Santos
Regis Santos

Reputation: 3749

Copying set of records in Django

How do I copy the data from one record to another? That is, I have the record

pk: 1,
name: "Regis"
total: "1000.00"

I wanted to create a new exactly the same record.

pk: 2,
name: "Regis"
total: "1000.00"

Upvotes: 0

Views: 101

Answers (1)

catavaran
catavaran

Reputation: 45575

Just set pk to None and save the record:

obj = MyModel.objects.get(pk=1)
obj.pk = None
obj.save()

This is an official method of copying model instances.

Upvotes: 3

Related Questions