Django How to apply bulk update on all child objects?

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.

Let’s break this down into two parts.

  • Create a query to get a queryset identifying all the rows to be updated.

  • Use the update method on that queryset to update those rows. (This can be done as a single statement)
    This works if you’re going to update all the rows with the same changes. Otherwise, see bulk_update.

1 Like

Thanks KenWhitesell. Finally I solved this issue after following your instruction.