#4027: ability to make a copies of model instances
---------------------------------------------+------------------------------
Reporter: Marek Kubica | Owner: nobody
Status: new | Milestone:
Component: Database wrapper | Version: SVN
Resolution: | Keywords: db, copy
Stage: Design decision needed | Has_patch: 1
Needs_docs: 1 | Needs_tests: 1
Needs_better_patch: 1 |
---------------------------------------------+------------------------------
Comment (by miracle2k):
With QSRF and model inheritance, setting the pk of a subclass model to
None is no longer enough to make a copy. From what I can tell, the best
working approach seems to be:
{{{
def copy_model_instance(obj):
from django.db.models import AutoField
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)
}}}
This is without handling m2m relationships.
--
Ticket URL: <http://code.djangoproject.com/ticket/4027#comment:8>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---