David542
David542

Reputation: 110163

How to call the django save method on an update

I would like to "disable" an update() method by essentially forcing it to call a save on every object in django. How would I do that? Currently I have:

  class HandleQuerySet(QuerySet):
      def update(self, *args, **kwargs):
          for x in self:
              x.save()

But this doesn't seem to be passing the args to the save method -- as in, it doesn't save anything. How would I properly do this?

Upvotes: 0

Views: 56

Answers (1)

NS0
NS0

Reputation: 6096

Do you want to update the fields that are passed in? You would need to update x with the arguments passed as kwargs. One way to do this is with the help of setattr:

class HandleQuerySet(QuerySet):
    def update(self, *args, **kwargs):
        for x in self:
            for k, v in kwargs.items():
                setattr(x, k, v)
            x.save()

Upvotes: 1

Related Questions