I want to learn how to copy an object but also copy the object referencing to that object.
As an example (simplified): Model 1: version
- id
- name
Model 2: file
- id
- file name
- file contents
- foreign key pointing to version
Relationship: One version can have multiple files
So one software version can have multiple files. I want to duplicate a complete version. Currently I have the following:
def duplicate_version(request,id, MAC_address):
new_version = Version.objects.get(pk=id)
new_version.pk = None
new_version.save()
new_files = File.objects.get(version_id=id) <-- here I get the error
new_id = new_version.id
new_files.version_id = new_id
new_files.save()
return get_all_versions(request, MAC_address)
I understand how to copy an object and change the id (=None). But how do I manage to duplicate all the related files?
Error I get: Exception Value: get() returned more than one File -- it returned 2!