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()
Source:stackexchange.com