Home > Software engineering >  Django How to bulk update on all child objects?
Django How to bulk update on all child objects?

Time:12-31

I am building help desk system on django. Where anyone can open ticket for customer support. Assume I have an parent object #001 and every child object of this parent have same ticket id. See the screenshot for better understand: enter image description here

child1 and child2 have same ticket id like their parent object. How to apply bulk update on all objects if they have same ticket id?. Assume if I change ticket status of child2 then I want it will also apply bulk update of child1 and parent object. any idea how to do that on django? here is my code:

models.py

class Contact(models.Model):
       choice = (("pending","pending"),("solved","solved"),("closed","closed"))
       ticket_status = models.CharField(choices=choice,max_length=100,default="pending")
       parent =models.ForeignKey('self', on_delete=models.CASCADE,
                  null=True, blank=True, related_name='contact_parent')
       sno = models.AutoField(primary_key=True,)


       def save(self,*args,**kwargs):
          if not self.parent and not self.support_ticket:
              self.support_ticket= str(rand_support_ticket())
          if not self.support_ticket:
               self.support_ticket = self.parent.support_ticket
          
       
          
          super(Contact,self).save(*args,**kwargs)
    

forms.py

class SupportAgentFrom(forms.ModelForm):
     class Meta:
         model = Contact
         fields = ['support_message','ticket_status']   

views.py

def AddReplySupport(request,slug):

    # fetch the object related to passed id
    obj = get_object_or_404(Contact, slug = slug)

    # pass the object as instance in form
    form = SupportAgentFrom(request.POST or None, instance = obj)   
    if form.is_valid():
        form.instance.support_agent = request.user
        form.save()

now I can update only single object once at a time. I want to apply bulk update on multiple objects at a time if they have same ticket id.

#Update1 Finally my problem is solved after following Dan Yishai solution. Here I want to try little bit explain his code so people can understand and solve this type of similar problems which I was facing.

Contact.objects.filter(
                      Q(support_ticket=form.instance.support_ticket) 
                  ).update( ticket_status="closed")

Above line of code searching and updating only those objects whose have exactly same ticket id.

CodePudding user response:

You can update the item and all of it's children in a single query, just replace your code inside the if with something like:

count = Contact.objects.filter(
    Q(pk=form.instance.pk) | Q(parent_id=form.instance.pk)
).update(support_agent=request.user)

You can use count to verify at least 1 object has been updated, and display to the user how many objects were modified.

CodePudding user response:

Below I'm assuming sno is the Ticket Id
To grab the queryset:

queryset = Contact.objects.filter(sno=form.instance.sno)

Now you can use .update() or .bulk_update().

Update every object to have the same support agent:

queryset.update(support_agent=request.user)

Update every object to have a different support agent:

for contact in queryset:
    contact.support_agent = value

queryset.bulk_update['support_agent']
  • Related