[Answered ]-Saving django model instance into another model

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()

Leave a comment