How to show django messages based on queryset filter

I want to show message based on my filter.


        ('0', 'published',),

        ('1', 'pending',),

        ('2', 'rejected',),


Here I tried this code for showing message based on filter but it’s only showing the published message.

def get_context_data(self, **kwargs):

            data = super().get_context_data(**kwargs)

            published = BlogComment.objects.filter(is_published="0")

            pending = BlogComment.objects.filter(is_published="1")


            if published:

                 messages.add_message(self.request, messages.INFO, 'Comment status published Sucessfully')

            elif pending: 

               messages.add_message(self.request, messages.INFO, 'Comment status pending Sucessfully')


            return data

why I am only getting the published message?

I also tried if statement instead of elif. After using if statement I am getting three message at time while changing status of any object.

            if published:
                 messages.add_message(self.request, messages.INFO, 'Comment Published Sucessfully')
            if pending: 
                 messages.add_message(self.request, messages.INFO, 'Comment Status Pending')
            if rejected:
                 messages.add_message(self.request, messages.INFO, 'Comment Status Rejected')

I aslo tried to use pk in filter but didn’t work.
BlogComment.objects.filter(is_published="0", pk=True)

I find out the solution. here I am sharing the code. hope it will help others.

def form_valid(self, form):

        self.object =

        status = self.object.is_published

        name =[0:20]

        messages.success(self.request, f"{name} comment status {status}")

        return super().form_valid(form)