I’m trying to duplicate an existing record in a PostgreSQL DB, it seems to be duplicating by increments of 2 each time I hit the duplicate button. If there’s 1 record in the database, once the button is hit it will create records 2 and 3.
Model
class Detail(models.Model):
created = models.DateTimeField(auto_now_add=True, blank=False)
last_update = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, related_name='+')
draft = models.BooleanField()
outage_name = models.ForeignKey(Outage, related_name='+')
group_name = models.CharField(max_length=100)
shift = models.CharField(max_length=6)
activity = models.CharField(max_length=100, null = False)
culture_title = models.ForeignKey(Culture, related_name='+')
work_completed = models.TextField()
work_planned = models.TextField()
radiation_info = models.TextField()
action_item = models.TextField()
lesson_learned = models.TextField()
View
def turnover_copy(request, id):
obj = Detail.objects.get(pk=id)
obj.pk = None
obj.draft = True
if obj.draft:
user = request.user.id
obj.user_id = user
obj.work_planned = 'My Work Planned.'
obj.save()
return HttpResponse('Created')
else:
return HttpResponse('Unable to duplicate template.')
EDIT: I had the def inside a for loop in the template, so it kept creating duplicates!
Are you sure the code isn’t called twice for some reason? Some print statements might help you assert that.