Ad

Delete Object From Database Automatically If One Field Is TRUE

- 1 answer

Automatically delete an object from the database if one attribute of the object is TRUE.

I've tried Django Signals, but it didn't help.

class Question(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField(max_length=50)
    question = models.TextField(max_length=200)
    answered = models.BooleanField(default=False)

    def __str__(self):
        return self.name

If I change the "answered" field to TRUE in Admin Panel, then this object must be automatically deleted from the database.

Ad

Answer

You will need post_save signals by using something like:

from .models import Question

from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=Question)
def save_profile(sender, instance, **kwargs):
    if instance.answered:
        instance.delete()
Ad
source: stackoverflow.com
Ad