Update Multiple Objects using SQL way in Django -
I have a comment model that requires admin approval. I am providing a view to admin to do this
However, I am using an expensive method to update the approval status.
comments = # Comment with a comment object for C: c.approved = true C.approved_on = #now c.save () As you can see, I am looping all the objects in the list and every time I am killing the database.
I am looking for a way in which I have to do something like SQL
as UPDATE comments SET Approved = 1 WHERE id (....... ....) This will do the same thing in a single SQL query.
Is there any way to do this in Django?
In this way, with the use of an approval method in the manager?
comment.blogs.filter (pk__in = [1,2,3]). Approved (). ()
Easiest way:
def Approval (self): back to self .get_queryset (). Update (Approved = true) Note : The problem with this solution is post_save and pre_save signals Will not be called.
If you need this signal, you will have to recreate items such as in your examples.
DIFF (self): qs = self.get_queryset () for qs in C: #Hit database! C.approved = true c.approved_on = #now c.save () return qs
Comments
Post a Comment