Last active
January 21, 2020 18:07
-
-
Save fredkingham/6ccec80b669ec2d378baa7c646b22633 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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