0

I have a queryset

Class SmallFoo(Model):
     text = models.CharField()

Class Foo(Model):
     small_foo = models.ManyToManyField(SmallFoo)

e.g.Foo.objects.filter(id__in=[2,4,6]).update(small_foo__remove=[1,2])

I want to do something like above i.e. for a query-set update the manytomany field for all of them. Is it possible to do that? I do not want to iterate over each object in queryset and fire separate queries for them. (It takes too much time)

Akamad007
  • 1,551
  • 3
  • 22
  • 38

1 Answers1

1

not sure if it helps your but you can try this

[f.small_foo.all().update(text="test") for f in Foo.objects.all()]

you still have to iterate over Foo but update all related SmallFoo at same time

zelenyjan
  • 663
  • 6
  • 7
  • Unfortunately I want to avoid doing the above. As I have 5000 Foo objects and iterating over them takes a few seconds and I want to avoid that. – Akamad007 Feb 14 '18 at 22:31