2👍
✅
I think that iterating over field list is more predictable way:
a = A.objects.get(id=1)
data = dict([field.name, getattr(a, field.name) for field in a._meta.fields])
b = B(**data)
b.pk = None
b.save()
Note: this doesn’t handle ManyToMany relationships. M2M fields should be copied manually.
0👍
There’s no need for any of that. Simply setting the id to None will cause it to save as a new instance.
a = A.objects.get(id=1)
a.id = None
a.save()
- [Answered ]-How to delete existing migrations in Django 1.7
- [Answered ]-UWSGI Emperor Mode Not Working Outside of Virtualenv
- [Answered ]-Django : Form data not saving
- [Answered ]-Django "lazy query execution" principle
- [Answered ]-Send Email Issue – Google Compute Engine VM
Source:stackexchange.com