Skip to content

Instantly share code, notes, and snippets.

@skyjur
Last active December 24, 2015 07:39
Show Gist options
  • Select an option

  • Save skyjur/6765347 to your computer and use it in GitHub Desktop.

Select an option

Save skyjur/6765347 to your computer and use it in GitHub Desktop.
Create copy of django model instance
def create_model_instance_copy(obj, **kwargs):
model = type(obj)
fields = [field for field in model._meta.fields if not field.primary_key]
initial = dict((field.get_attname(), field.value_from_object(obj)) for field in fields)
initial.update(**kwargs)
return model.objects.create(**initial)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment