Skip to content

Instantly share code, notes, and snippets.

@fredkingham
Last active January 21, 2020 18:07
Show Gist options
  • Save fredkingham/6ccec80b669ec2d378baa7c646b22633 to your computer and use it in GitHub Desktop.
Save fredkingham/6ccec80b669ec2d378baa7c646b22633 to your computer and use it in GitHub Desktop.
from opal.models import Episode
from opal.core.subrecords import episode_subrecords
from django.db import transaction
@transaction.atomic
def clone_episode(old):
new = Episode(
patient=old.patient,
active=old.active,
start=old.start,
end=old.end,
category_name=old.category_name,
stage=old.stage
)
new.save()
for sub in episode_subrecords():
if sub._is_singleton:
sub.objects.filter(episode=new).delete()
for item in sub.objects.filter(episode=old):
item.id = None
item.episode = new
item.save()
old.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment