how to check if a post status was changed from live to cancel in django?

i want to write a condition that would show a warning message when a user try to change the status of a post to cancel , if the post already have a participant in it. The conditional statement seems not to be working.

It sets the post to cancel even if there is already a participant. what i want is this, provided there is already at least 1 participant in the object, i want to show a warning message that they cannot cancel the post and then i redirect them back to the dashboard.

class PredictionUpdate(ProductInline, UpdateView):
    def form_valid(self, form):
        instance = form.instance
        if instance.participants.exists() and instance.status == 'cancelled':
            messages.warning(self.request, "You cannot cancel a bet that already have participants")
            return redirect("core:dashboard")
        else:
            form.save()
            return HttpResponseRedirect(self.get_success_url())

complete code

class PredictionUpdate(LoginRequiredMixin, ProductInline, UpdateView):

    def get_context_data(self, **kwargs):
        ctx = super(PredictionUpdate, self).get_context_data(**kwargs)
        ctx['named_formsets'] = self.get_named_formsets()
        return ctx
    
    def get_named_formsets(self):
        return {
            'variants': PredictionDataFormSet(self.request.POST or None, self.request.FILES or None, instance=self.object, prefix='variants'),
        }

    def dispatch(self, request ,*args, **kwargs):
        obj = self.get_object()
        if obj.user != self.request.user:
            messages.warning(self.request, "You are not allowed to edit this bet!")
            return redirect("core:dashboard")
        
        if obj.status == "finished":
            messages.warning(self.request, "You cannot edit this bet again, send an update request to continue.")
            return redirect("core:dashboard")
            # raise Http404("You are not allowed to edit this Post")
        return super(PredictionUpdate, self).dispatch(request, *args, **kwargs)
    
    def get_success_url(self):
        return reverse("core:dashboard")

    
    def form_valid(self, form):
        instance = form.instance
        if instance.participants.exists() and instance.status == 'cancelled':
            messages.warning(self.request, "You cannot cancel a bet that already have participants")
            return redirect("core:dashboard")
        else:
            form.save()
            return HttpResponseRedirect(self.get_success_url())

It’s not clear from this post what form or model is being worked on here. We’ll need to see those to understand what’s going on.

This is the model that is been worked on


class Predictions(models.Model):
    prediction_topic = models.ForeignKey(PredictionTopic, on_delete=models.SET_NULL, null=True, blank=True, related_name="prediction_topic")
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    status = models.CharField(choices=STATUS, max_length=100, default="in_review")
    participants = models.ManyToManyField(User, related_name="participants", blank=True)

This is the form


class PredictionForm(forms.ModelForm):
    image = ImageField(widget=FileInput)
    class Meta:
        model = Predictions
        fields = [
            'prediction_topic',
            'status',
        ]

What are the STATUS choices?

These are the STATUS

STATUS = (
    ("live", "Live"),
    ("in_review", "In review"),
    ("pending", "Pending"),
    ("cancelled", "Cancelled"),
    ("finished", "Finished"),
)

I’m not seeing anything obviously wrong with what you’ve posted. You might want to try to debug this by adding some print statements within your form_valid function before the if statement to show what the current values are of instance.participants and instance.status.

1 Like

This guided me to fix the problem, thanks alot.