[Django]-How to copy and change a property of many models from a queryset, in batch?

0👍

I don’t think there is an easy way to do this without iterating over the entire queryset.

One way to make this function more efficient is to use transation.atomic(), like this:

def save(self, **kwargs):
    super(User, self).save(**kwargs)
    with transation.atomic():
        for c in MyModelCategory.objects.filter(mymodel__is_default=True):
            c.pk = None
            c.user = self.user
            c.save()
            for s in MyModel.objects.filter(category=c):
                s.pk = None
                s.user = self.user
                s.save()

Leave a comment