Copy a model instance

1
2
3
4
5
6
7
from django.db.models import AutoField
def copy_model_instance(obj):
    initial = dict([(f.name, getattr(obj, f.name))
                    for f in obj._meta.fields
                    if not isinstance(f, AutoField) and\
                       not f in obj._meta.parents.values()])
    return obj.__class__(**initial)

More like this

  1. Per-Instance On-Model M2M Caching by bryanhelmig 1 year, 6 months ago
  2. ModelForm Class saving m2m by ckarrie2 4 months ago
  3. Clone model mixin by zakj 3 years, 1 month ago
  4. How to connect m2m_changed sent by specific senders for symmetrical m2ms by ilikefm 3 months, 1 week ago
  5. load m2m fields objects by dirol 1 year, 8 months ago

Comments

(Forgotten your password?)