Last active
December 24, 2015 07:39
-
-
Save skyjur/6765347 to your computer and use it in GitHub Desktop.
Create copy of django model instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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