When you use an intermediate through
table your ability to use the related objects manager (i.e. obj.clowns
) is limited. As the docs put it:
Unlike normal many-to-many fields, you can’t use add, create, or assignment (i.e., beatles.members = [...]) to create relationships.... The simple add, create and assignment calls don’t provide a way to specify this extra detail. As a result, they are disabled for many-to-many relationships that use an intermediate model. The only way to create this type of relationship is to create instances of the intermediate model.
So instead of assigning the related manager, just create the new intermediate objects yourself:
def duplicate(self, request, queryset):
for obj in queryset:
old_workedat = list(obj.workedat_set.all())
obj.id = None
obj.name += ' (copy)'
obj.save()
new_workedat = [WorkedAt(circus_id=obj.id, clown_id=wa.clown_id)
for wa in old_workedat]
WorkedAt.objects.bulk_create(new_workedat)