[Answered ]-Duplicate Django model instance without modifying python object

1👍

Refreshing from db with correct id do the job, Downside is having to do multiple database call.

    def duplicate_from_db(self):
        old_pk = self.pk  # storing for refreshing latter 
        self.pk = None
        self._state.adding = True
        self.save()
        new_pk = self.pk
        self.pk = old_pk
        self.refresh_from_db()
        return MyModel.objects.get(pk=new_pk)

Leave a comment